views:

95

answers:

4

What I want


If the URL in the string contains a .jpg at the end of the URL (not the string) then it should make an image from it with preg_replace else make a normal link.

so for example:

If I have http://www.example.com/images/photo.jpg then it should replace with:

<img src="http://www.example.com/images/photo.jpg" alt="http://www.example.com/images/photo.jpg"&gt;

The problem:


The URL is replaced with a link in any way and my regex isn't working :( .

What I have tried:


        $content = preg_replace("/(http:\/\/[^\s]+(?=\.jpg))/i","<img src=\"$1\" alt = \"$1\"></img>",$content);    

        $content = nl2br(preg_replace("/(http:\/\/[^\s]+(?!\.jpg))/m", "<a href=\"$1\" rel=\"nofollow\" target=\"blank\" title=\"$1\" class=\"news-link\">$1</a>", $content));
+1  A: 
$content = preg_replace('#\b(http://\S+\.jpg)\b#i', '<img src="$1" alt="$1" />', $content);
reko_t
`\b` matches before/after periods and other symbols: so it would match `http://something.com/image.jpg.html` and the like. And don't you need to escape those backslashes?
Amarghosh
same problem with Amargohosh's version.
CIRK
A: 

You don't need lookaround. Just go with

$content = preg_replace("#(http://[^ ]+\\.jpg(?= |$)#i","<img src=\"$1\" alt=\"$1\"/>", $content);    
Amarghosh
If the image replacement is before the link replacement then shows it very badly, if its after the link replacement then it's just a link, what should I do?
CIRK
this is what I get if the image replacement is before the link replacement: `http://192.168.2.104/images/users/image.jpg" alt = "" rel="nofollow" target="blank" title="http://192.168.2.104/images/users/image.jpg">" class="news-link">http://192.168.2.104/images/users/image.jpg">`
CIRK
+1  A: 

I think you used the lookahead operator when you wanted lookbehind. You could change (?=\.jpg) to (?<=\.jpg) but there are other, cleaner regex's I'm sure others will post.

Brandon Horsley
but I want to find .jpg after the link
CIRK
+1  A: 

Try this

function replace_links($content)
{
    if (preg_match('#(http://[^\s]+(?=\.(jpe?g|png|gif)))#i', $content))
    {
        $content = preg_replace('#(http://[^\s]+(?=\.(jpe?g|png|gif)))(\.(jpe?g|png|gif))#i', '<img src="$1.$2" alt="$1.$2" />', $content);
    }
    else
    {
        $content = preg_replace('#(http://[^\s]+(?!\.(jpe?g|png|gif)))#i', '<a href="$1" rel="nofollow" target="blank" title="$1" class="news-link">$1</a>', $content);
    }

    return $content;
}
BDuelz
Hey! Thanks for your answer :), I've tried a similar solution with `IF/THEN`, but yours is better ;), it works, but there are 2 problems with it, 1. in this way I can't have any links which is not ending in `jpg`, `png` or `gif` in the `content`, the 2. is that after the photos there is the image type `.jpg` in plus so ends with `<img src="http://example.com/image.jpg" alt="http://example.com/image.jpg">.jpg`, I don't need that and I don't know how to get rid of them as I see you've put a $2 in the source and alt, I've deleted them, than disappears but the image is not shown o.O
CIRK