Page 1 of 1

Negative Assertions help...

Posted: Sun Mar 02, 2008 8:34 pm
by Mr Tech
Basically what I want to do is add a url before any image tags that don't already have a url in front of them... So for example, this:

Code: Select all

<img src="images/logo.jpg" border="0" />
Would be replaced with this:

Code: Select all

<img src="http://www.domain.com/images/logo.jpg" border="0" />
Here is my code... it works when the image tag already has a url before the image source but doesn't apply the domain to images that don't...

Code: Select all

$websiteurl = 'http://www.domain.com';
$content = preg_replace('#src="([^"]+?!http://|!https://)#im', "src=\"".$websiteurl."/$1", $content);
Any ideas how I can fix my code to make this work?

Re: Negative Assertions help...

Posted: Sun Mar 02, 2008 9:08 pm
by Christopher
You might be able to just use str_replace().

Code: Select all

$websiteurl = 'http://www.domain.com';
$content = str_replace('src="images/', "src=\"$websiteurl/images/", $content);

Re: Negative Assertions help...

Posted: Sun Mar 02, 2008 9:46 pm
by Mr Tech
Unfortunately the folder that contains the image may not always be called images.. it could be anything...

Re: Negative Assertions help...

Posted: Mon Mar 03, 2008 8:23 pm
by HCBen
Try this pattern:

Code: Select all

#src="([^"]*/)([^"]*)"#i
The first paren will match anything up to (and including) the last forward slash "/", which, if I understand you correctly, is what you need to replace with $websiteurl. The second paren just matches the rest of the string, which would be the name of the image (e.g. "logo.jpg").

Cheers!
Ben

Re: Negative Assertions help...

Posted: Mon Mar 03, 2008 9:08 pm
by Mr Tech
Hey Ben! Thanks for that but that's not exactly what I'm looking for... Let me explain a little better:

If there is already a http:// or https:// in front of the image source, I want to leave it as is... but if the image source looks like this:

Code: Select all

<img src="images/logo.jpg">
It needs to be replaced with:

Code: Select all

<img src="http://www.domain.com/images/logo.jpg">
If the image source looks like this:

Code: Select all

<img src="uploads/files/ben.jpg">
It needs to be replaced with:

Code: Select all

<img src="http://www.domain.com/uploads/files/ben.jpg">
If the image source looks like this:

Code: Select all

<img src="http://www.google.com/images/pic.jpg">
It needs to be left as is:

Code: Select all

<img src="http://www.google.com/images/pic.jpg">
Am I making sense?

Re: Negative Assertions help...

Posted: Mon Mar 03, 2008 10:03 pm
by HCBen
Ok, then you'll need to use preg_match_all (http://us2.php.net/manual/en/function.p ... ch-all.php).

Essentially you're needing to append the $websiteurl to the initial quote in every image src attribute - but only if "http" doesn't already exist in the src.

Try this:

Code: Select all

 
if (preg_match_all("#(src=")([^"]*)"#i","$content",$matches, PREG_PATTERN_ORDER))
{
    foreach ($matches as $match)
    {
        if (stripos($match[2],'http') === FALSE)
        {
            $src = $match[1] . $websiteurl . $match[2] . '"';
            $content = str_ireplace($match[0], $src, $content);
        }
    }
}
This is just off the top of my head without testing it, but it looks right. :wink:
Give it a shot.

Cheers!
Ben

Re: Negative Assertions help...

Posted: Mon Mar 03, 2008 10:48 pm
by Mr Tech
Thanks Ben, really appreciate your help :)

I tried your code and it's almost working. I'm running PHP 4 :roll: so I had to use a couple of alternatives for the case-sensitive functions...

This is the result of the code on an image:

Code: Select all

<img width="300" hspace="5" height="225" align="right" src="src="http://www.domain.com"src="src="http://www.domain.com"uploads/images/Sunset.jpg"http://www.domain.com" alt="" />
Here is the result on the second image from the array... for some reason it's a little better...:

Code: Select all

<img width="100" height="75" src="src="http://www.domain.com"uploads/images/Sunset.jpg" alt="" />
Here is the print_r() of $matches:

Code: Select all

Array
(
    [0] => Array
        (
            [0] => src="uploads/images/Blue hills.jpg"
            [1] => src="uploads/images/Sunset.jpg"
        )
 
    [1] => Array
        (
            [0] => src="
            [1] => src="
        )
 
    [2] => Array
        (
            [0] => uploads/images/Blue hills.jpg
            [1] => uploads/images/Sunset.jpg
        )
 
)
Array
(
    [0] => Array
        (
            [0] => src="uploads/images/Blue hills.jpg"
        )
 
    [1] => Array
        (
            [0] => src="
        )
 
    [2] => Array
        (
            [0] => uploads/images/Blue hills.jpg
        )
 
)
 
 
 
Here is the print_r() of $match in the foreach() function

Code: Select all

Array
(
    [0] => src="uploads/images/Blue hills.jpg"
    [1] => src="uploads/images/Sunset.jpg"
)
Array
(
    [0] => src="
    [1] => src="
)
Array
(
    [0] => uploads/images/Blue hills.jpg
    [1] => uploads/images/Sunset.jpg
)
Array
(
    [0] => src="uploads/images/Blue hills.jpg"
)
Array
(
    [0] => src="
)
Array
(
    [0] => uploads/images/Blue hills.jpg
)
 
 
And here is the final code, I had to had some back slashes to the preg_match_all() function but I think that's all I fiddled with...

Code: Select all

           if (preg_match_all("#(src=\")([^\"]*)\"#i", $content, $matches, PREG_PATTERN_ORDER)) {
                foreach ($matches as $match) {
                    if (stripos($match[2],'http') === FALSE) {
                        $src = 'src="' . $match[1] . $websiteurl . $match[2] . '"';
                        $content = str_ireplace($match[0], $src, $content);
                    }
                }
            }