View Full Version : Find and replace, extracting variables

03-17-2009, 12:49 AM
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:


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;

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


$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