hi there
I'm new to php but im trying to replace items on page with a certain id with a image or anything i want. So i have this code on the page

[vimeo 123456]
[vimeo 345675]

my php goes off to vimeo to get the correct image based on the number. but my problem is when i use preg_replace it only every does the same image over each item. So if I have 2 tags on the page I get 2 images but they are the same image.

here is my php code

function vimeo_code($data) {

$replace = "#\[vimeo (.*?)\]#i";
$v_id = preg_match_all($replace, $data, $matches,PREG_PATTERN_ORDER);

 function curl_get($url) {
	$curl = curl_init($url);
	curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($curl, CURLOPT_TIMEOUT, 30);
	$return = curl_exec($curl);
	curl_close($curl);
	return $return;
}


foreach ($matches[1] as $item){
if (!isset($vimeo_video[$item])){
$vimeo_video[$item]=true;


$api_endpoint = 'http://www.vimeo.com/api/v2/video/'.$item;
$oembed_endpoint = 'http://www.vimeo.com/api/oembed.xml';




$videos = simplexml_load_string(curl_get($api_endpoint.'.xml')); 


$video_url = $videos->video->url;
$video_thumbnail_large = $videos->video->thumbnail_large;
$video_title = $videos->video->title;
echo('pp'.$video_thumbnail_large.$video_title);
echo($item);

$data = preg_replace($replace,"<img src=\"$video_thumbnail_large\"",$data);

	}
}
echo($data);

}
add_filter('the_content', 'vimeo_code');

it echos the correct images and ids out but not preg_replace

any help would be great

cheers
andy

Recommended Answers

All 3 Replies

Your regex should be as follows:

$replace = "#\[vimeo[^\]]+([0-9]+)\]#is";

u can also try

$replace = "#\[vimeo[^\]]+*+)\]#is";

hi
the $replace wasnt the problem with it. I did try what was suggested just to see what it did but it didnt remove the correct string i wanted.

I have fixed the problem i had to pass

foreach ($matches[1] as $item)

into an array $vid[] then access the $matches[0] outside of the foreach and use the array $vid as the replace.

It now takes the contents off the page and replaces each bit correct in the right place. Had it doing some weird things while trying to get it to work. I think vimeo also banned my ip address a few times from making bad request too many times. so had to disconnect and reconnect.

heres the code if it helps anyone else out.

function vimeo_code($data) {

$replace = "~\[vimeo (.*?)\]~i";

$v_id = preg_match_all($replace, $data, $matches,PREG_PATTERN_ORDER);

foreach($matches[1] as $id){

$api_endpoint = 'http://www.vimeo.com/api/v2/video/'.$id;
$oembed_endpoint = 'http://www.vimeo.com/api/oembed.xml';
$videos = simplexml_load_string(curl_get($api_endpoint.'.xml'));
$video_thumbnail_large = $videos->video->thumbnail_large;
$vimage = "<img src=\"$video_thumbnail_large\"";
$vid[]= $vimage;

}
$data = str_replace($matches[0],$vid,$data);
		
return($data);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.