...

View Full Version : Find and replace, extracting variables



ninexn
03-16-2009, 11:49 PM
I currently have a script that inserts a video play on my blog, it looks for [video=11] 11 being the video id.

I want to be able to have video variables, such as:

[video=11&w=600&h=333]

and then have each variable $filename, $width, $height.


Any ideas?

This is the code i am using.


add_filter('the_content', 'ninexn_the_content');
function ninexn_the_content( $content )
{
// find video id
$search = "/\[video=(.*)\]/";
preg_match_all($search, $content, $video_matches);

if (is_array($video_matches[1]))
{
foreach ($video_matches[1] as $filename)
{
$search = "[video=".$filename."]";


$replace = ninexnvideo($filename);
$content = str_replace ($search, $replace, $content);

}
}

return $content;
}

Iszak
03-17-2009, 12:03 AM
I'm confused on what you want to do but here's something that'll allow the extra variables.



<?php

$string = '[video=11&w=600&h=333]';

preg_match_all('#\[video=(\d+)&w=(\d+)&h=(\d+)\]#i', $string, $matches);


if (is_array($matches[1]) === true)
{
$i = 0;
foreach ($matches[1] as $filename)
{
$width = $matches[2][$i];
$height = $matches[3][$i];

// Do whatever you need

$i++;
}
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum