Jump to content

Recommended Posts

 

Currently I have a large string of html stored in a variable. Within the html is some images.I'm using preg_replace to find all the images

$content = preg_replace('~'.$site.'(\S+\.(?:jpe?g|png|gif))~i', "$1",$content);

I'm wanting to run a function on every image to cache it locally. So I need to run a function on each string preg_replace picks out. Do even need to be using reg_replace, or should I be using something else? How do i do this?

 

Is there any reason you're actually using preg_replace(), do you want to replace things in a string, or just pick parts out? Perhaps a combination of preg_match_all() and array_map() will work for you. Something like:

 

$content = preg_match_all('~'.$site.'(\S+\.(?:jpe?g|png|gif))~i', $content, $matches);
array_map('some_function', $matches[0]);

Is there any reason you're actually using preg_replace(), do you want to replace things in a string, or just pick parts out? Perhaps a combination of preg_match_all() and array_map() will work for you. Something like:

 

$content = preg_match_all('~'.$site.'(\S+\.(?:jpe?g|png|gif))~i', $content, $matches);
array_map('some_function', $matches[0]);

 

I tried that and wrote some code, but I'm still not exactly how to get it to work. I want to pick out the image src, run a function on it to cache it, then display it as if normally. How do i get it to change the src of the image through my function? I've tried some str_replace and other things with no luck. This is what i have so far

preg_match_all('~'.$site.'(\S+\.(?:jpe?g|png|gif))~i', $content, $matches);
array_map('cache', $matches);		


function cache($matches){
	$img = $matches[0].'<br>';
	echo $img;
	$matches = 'src="http://a3.twimg.com/profile_images/195726221/Kidguru_normal.jpg';
	$matches[0] = $matches;
}

Can you preform print_r($matches) before array_map and post the results please.

 

This post has a lot of images...

Array ( [0] => Array ( [0] => src="http://api.tweetmeme.com/imagebutton.gif [1] => src="http://cdn.mashable.com/wp-content/uploads/2009/11/swine-flu-shot.jpg [2] => src="http://cdn.mashable.com/wp-content/uploads/2009/11/swineflu-graph.jpg [3] => src="http://cdn.mashable.com/wp-content/uploads/2009/11/ilicdc-graph.jpg [4] => src="http://cdn.mashable.com/wp-content/uploads/2009/11/swineflu-twitter.jpg ) [1] => Array ( [0] => src="http://api.tweetmeme.com/imagebutton.gif [1] => src="http://cdn.mashable.com/wp-content/uploads/2009/11/swine-flu-shot.jpg [2] => src="http://cdn.mashable.com/wp-content/uploads/2009/11/swineflu-graph.jpg [3] => src="http://cdn.mashable.com/wp-content/uploads/2009/11/ilicdc-graph.jpg [4] => src="http://cdn.mashable.com/wp-content/uploads/2009/11/swineflu-twitter.jpg ) ) 

 

Also I'm getting

Fatal error: Cannot redeclare cache() (previously declared in /home/kidguru/public_html/thetechworld/functions/remove_unwanted_post_content.php:69) in /home/kidguru/public_html/thetechworld/functions/remove_unwanted_post_content.php on line 72

after the $content has been echo'd

To get rid of the src=" you could just do $img = substr($matches[0], 5), but ideally you should get a better regular expression.

 

I don't necessarily need to do that do I?

 

All I want to do i locate images in a blog post, run a function on the image, update the image location in the post.

 

I would like to be something like

		preg_match_all('~'.$site.'(\S+\.(?:jpe?g|png|gif))~i', $content, $matches);
array_map('cache', $matches);		


function cache($matches){
	$new_location = cache_img($matches[0]);

	$content = str_replace($matches[0], $new_location, $content);
}

function cache_img(){
	// Cache image code //	
}

 

Would it help if you could see it? http://www.thetechworld.org/?user=wee493

look preg_replace_callback() function

I tried that and now i'm getting

Fatal error: Cannot redeclare cache() (previously declared in /home/kidguru/public_html/thetechworld/functions/remove_unwanted_post_content.php:68) in /home/kidguru/public_html/thetechworld/functions/remove_unwanted_post_content.php on line 73

 

And it's still not working

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.