Rootbeer Posted November 5, 2010 Share Posted November 5, 2010 Here is what I Am trying to do: Fetch an RSS feed and save it on my server Find all the images in the feed (in the description field Find out the size of the images, calculate a new size keeping the same aspect ratio Update the height/width field of the image in the original feed Heres what I have done so far: Saved the feed in my server Found all the images using preg_match_all() Extracted the image URL using preg_match() Calculated the new size Now how do I update the feed saved in the server? Seems like I have to find the <img> tags again in the feed using regexp and replace them with the new updated <img> tags. I feel like this is not the best way to do it. Can anyone help me out with a more efficient way? Thanks! p.s. Heres my code: // Now find and resize image $regex = '#<img.*src=\".*\"\s\/>#'; preg_match_all($regex, $text, $matches); foreach($matches[0] as $value) { // Extract the image url from the <img> tag $regex = '#http:\/\/www\.website.com\/.*\.(jpg|jpeg|gif|png)#'; preg_match($regex, $value, $imgurl); // Edit $value here. Now want to put $value back into $text // Now run another regexp here searching $text for old $value and replace it with the new $value? } Quote Link to comment Share on other sites More sharing options...
.josh Posted November 5, 2010 Share Posted November 5, 2010 look into preg_replace_callback. It can extract data and replace it with something else as it finds it, and you can supply a function to be called (passing the extracted info, returning what to replace) each iteration, so you can perform your calculations in the callback function (or call some other function that does it, w/e). Quote Link to comment Share on other sites More sharing options...
Rootbeer Posted November 5, 2010 Author Share Posted November 5, 2010 Thank you very much Quote Link to comment 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.