You are here:  » search and replace with regular expression


search and replace with regular expression

Submitted by dflsports on Fri, 2006-01-27 18:52 in

Worms all over the place.

I have the search and replace working for URL's now I have another problem with 4 or 5 feeds I extract from xml, the image field has the image but it is inside html tags

I can do this bit of code

$product["THUMB"] = str_replace("<img border=0 width=50 height=50 src="/,"",$product["THUMB"]);
$product["THUMB"] = str_replace(">","",$product["THUMB"]);

But if the width and height are not = 50 then it does not get replaced. I searched the web high and low and found some stuff that had something to do woth preg replace and something with regexp like

$regexp = '/(<img[^>]*src=")([^"]*)("[^>]*>)/i';

Nothing I try works, go figure, that's a big suprise ;)

Any suggestions?

[mod: title edited for clarity]

Submitted by support on Fri, 2006-01-27 20:44

Hi Don,

You could do it something like this:

<?php
  $product
["THUMB"] = '<img width="25" height="60" src="http://www.example.com/image.gif">';
  
$url_start strpos($product["THUMB"],"src=")+5;
  
$url_end strpos($product["THUMB"],">")-1;
  
$product["THUMB"] = substr($product["THUMB"],$url_start,($url_end $url_start));
  print 
$product["THUMB"];
?>

Hope this helps!
David.

Submitted by dflsports on Sat, 2006-01-28 00:46

Again, your my hero :)

I had to modify it a little, it was a total guessing game for me,

$url_start = strpos($product["THUMB"],"src=")+4;
$url_end = strpos($product["THUMB"],">");
$product["THUMB"] = substr($product["THUMB"],$url_start,($url_end - $url_start));

is what I have (I did not need to print since it's being imported inot the mysql database.

If your reading this and looking for a great import script and great support, buy this script now!