wee493 Posted November 14, 2009 Share Posted November 14, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/181530-need-to-run-function-on-parts-of-text-string/ Share on other sites More sharing options...
Alex Posted November 14, 2009 Share Posted November 14, 2009 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]); Quote Link to comment https://forums.phpfreaks.com/topic/181530-need-to-run-function-on-parts-of-text-string/#findComment-957552 Share on other sites More sharing options...
wee493 Posted November 15, 2009 Author Share Posted November 15, 2009 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; } Quote Link to comment https://forums.phpfreaks.com/topic/181530-need-to-run-function-on-parts-of-text-string/#findComment-957780 Share on other sites More sharing options...
Alex Posted November 15, 2009 Share Posted November 15, 2009 Can you preform print_r($matches) before array_map and post the results please. Quote Link to comment https://forums.phpfreaks.com/topic/181530-need-to-run-function-on-parts-of-text-string/#findComment-957784 Share on other sites More sharing options...
wee493 Posted November 15, 2009 Author Share Posted November 15, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/181530-need-to-run-function-on-parts-of-text-string/#findComment-957788 Share on other sites More sharing options...
Alex Posted November 15, 2009 Share Posted November 15, 2009 To get rid of the src=" you could just do $img = substr($matches[0], 5), but ideally you should get a better regular expression. Quote Link to comment https://forums.phpfreaks.com/topic/181530-need-to-run-function-on-parts-of-text-string/#findComment-957789 Share on other sites More sharing options...
wee493 Posted November 15, 2009 Author Share Posted November 15, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/181530-need-to-run-function-on-parts-of-text-string/#findComment-957794 Share on other sites More sharing options...
sasa Posted November 15, 2009 Share Posted November 15, 2009 look preg_replace_callback() function Quote Link to comment https://forums.phpfreaks.com/topic/181530-need-to-run-function-on-parts-of-text-string/#findComment-957801 Share on other sites More sharing options...
wee493 Posted November 15, 2009 Author Share Posted November 15, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/181530-need-to-run-function-on-parts-of-text-string/#findComment-957844 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.