0

Here is my code:

$str = '<html><p><img src="http://test.com/images.jpg" /><img src="test.com/image2.jpg"><p><html>';
$str_rep = str_replace('/^(http|https|ftp):\/\/([A-Z0-9][A-Z0-9_-]*(?:\.[A-Z0-9][A-Z0-9_-]*)+):?(\d+)?\/?/i','http://mywebsite.com/', $str);

I want to replace the image URL using a regular expression. How can I do this?

Hai Truong IT
  • 4,126
  • 13
  • 55
  • 102
  • 1
    Are you sure you meant **str** _replace? – zerkms Dec 15 '11 at 04:51
  • @zerkms: you can using other function()(not use str_replace()), how to ideas? – Hai Truong IT Dec 15 '11 at 04:55
  • Trust me, you don't want to replace the image URL with a *regular expression*, you want to [parse the HTML](http://stackoverflow.com/questions/3577641/best-way-to-parse-html-with-php) and then manipulate the DOM. – casperOne Dec 15 '11 at 17:19

1 Answers1

1

Use preg_replace($regex, $replace_with, $string) to match and replace using regular expressions.

preg_replace('/^(http|https|ftp):\/\/([A-Z0-9][A-Z0-9_-]*(?:\.[A-Z0-9][A-Z0-9_-]*)+):?(\d+)?\/?/i', 'http://mywebsite.com/', $str);

preg_replace in PHP manual.