0

How can i use ereg to replace URL of images with tag.
My images URL have two starts embraced like this **http://myimageslink/photo.jpg**
i want to convert to <img src="http://myimageslink/photo.jpg"></img>

I use ** embrace because when don't have ** https://www.google.com/?gws_rd=ssl i will convert to <a href="https://www.google.com/?gws_rd=ssl">https://www.google.com/?gws_rd=ssl</a> like this.

The code below doesn't work i got like this <img src="**http://myimageslink/photo.jpg**"></img>

   $text = ereg_replace("**[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]**",
                             "<img src=\"\\0\"></img>", $str);

but I want the results like this.

<img src="http://myimageslink/photo.jpg"></img>

And this the real example.

$text  = 'This is first photo **http://myimageslink/photo.jpg**<br />
        This is second photo **http://myimageslink/photo.jpg**';
$text  = ereg_replace("**[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]**",
                             "<img src=\"\\0\"></img>", $text);

So, $text should be

$text = 'This is first photo <img src="http://myimageslink/photo.jpg"><br />
        This is second photo </img><img src="http://myimageslink/photo.jpg"></img>';
Marcus
  • 295
  • 4
  • 16
  • 1
    Sidenote: From the manual [`ereg_replace()`](http://www.php.net/manual/en/function.ereg-replace.php) --- **Warning** This function has been *DEPRECATED* as of PHP 5.3.0. Relying on this feature is highly discouraged. **Tip** ereg_replace() is deprecated as of PHP 5.3.0. [`preg_replace()`](http://www.php.net/manual/en/function.preg-replace.php) is the suggested alternative to this function. – Funk Forty Niner Jun 29 '14 at 02:07
  • As @Fred-ii- said `ereg_` has been deprecated for a long time now. Use `preg_` functions instead. – samayo Jun 29 '14 at 02:09
  • how can i use them for my question, suggest me for that, please,. – Marcus Jun 29 '14 at 02:11
  • 1
    See SO's guide => http://stackoverflow.com/q/6270004/ – Funk Forty Niner Jun 29 '14 at 02:15
  • Regardless of the advised switch, use capture groups, and not `\\0` to reinsert the whole match. – mario Jun 29 '14 at 03:12

0 Answers0