0

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

3
Contributors
3
Replies
4
Views
7 Years
Discussion Span
Last Post by andy_brain
0

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);
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.