Adding website url to images

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
User avatar
Mr Tech
Forum Contributor
Posts: 424
Joined: Tue Aug 10, 2004 3:08 am

Adding website url to images

Post by Mr Tech »

I have the following code below on my website. It's used to find the images in a block of html that don't have http:// or / in front. If this is the case, it will add the website url to the front of the image source.

E.g:

<img src="http://domain.com/image.jpg"> will stay the same
<img src="/image.jpg"> will stay the same
<img src="image.jpg"> will be changed to <img src="http://domain.com/image.jpg">

I feel my code is really inefficient... Any ideas on how I could make it run with less code?

Code: Select all

   preg_match_all('/<img[\s]+[^>]*src\s*=\s*[\"\']?([^\'\" >]+)[\'\" >]/i', $content_text, $matches);
        if (isset($matches[1])) {
            foreach($matches[1] AS $link) {
                if (!preg_match("/^(https?|ftp)\:\/\//sie", $link) && !preg_match("/^\//sie", $link)) {
                    $full_link = get_option('siteurl') . '/' . $link;
                    $content_text = str_replace($link, $full_link, $content_text);
                }
            }
        }
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Adding website url to images

Post by AbraCadaver »

You can combine all nine lines of code into one preg_replace(). Definitely not tested, but it may get you started. I just combined your patterns using a negative lookbehind:

Code: Select all

$content_text = preg_replace("/<img[\s]+[^>]*src\s*=\s*[\"']?(?<!https?|ftp|\/)([^\"' >]+)[\"' >]/si", $full_link . "/\\1", $content_text);
-Shawn
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
ridgerunner
Forum Contributor
Posts: 214
Joined: Sun Jul 05, 2009 10:39 pm
Location: SLC, UT

Re: Adding website url to images

Post by ridgerunner »

AbraCadaver was heading down the right track, but you need to use negative lookahead. (You also need to save the first chunk of the IMG tag so that you can piece it back together with the replace operation.) Here's a solution that does the trick in one whack:

Code: Select all

$content_text = preg_replace('%
    (<img\b[^>]*?src\s*+=\s*+)  # capture first chunk of IMG tag in group 1
    ("|\\'|)                     # capture SRC attribute delimiter in group 2
    (?!\w++://|/)               # ensure URL has no scheme nor abs path
    ([^"\\' >]++)                # capture relative URI path in group 3
    \2                          # match closing delimiter for SRC attribute
    %ix', '$1"http://domain.com/$3"', $content_text);
User avatar
Mr Tech
Forum Contributor
Posts: 424
Joined: Tue Aug 10, 2004 3:08 am

Re: Adding website url to images

Post by Mr Tech »

Perfect! Just what I needed! Thank you both :)
Post Reply