user2444411
user2444411

Reputation: 75

Validate img url with Regular Expression

I have been trying to validate a image url using regular expression like so:

#\[img\](((http:\/\/|https:\/\/)?(www.)?(([a-zA-Z0-9-]){2,}\.){1,4}([a-zA-Z]){2,6}(\/([a-zA-Z-_\/\.0-9#:?=&;,]*)?)?))\[\/img\]#iUs

i want be to able to replace:

[img]http://www.website/folders/url_here.png[/img]
[img]https://www.website/folders/url_here.jpg[/img]
[img]https://www.website/folders/url_here.gif[/img]

with:

<img src="http(s):www.website/folders/url_here.png(jpg, gif)" />

Using:

$new_string = preg_replace($search, $replace, $string);

Thank you!

Upvotes: 2

Views: 254

Answers (3)

Livewire
Livewire

Reputation: 180

You will want to use something like:

$output = preg_replace('/\[img\]((?:https|http|ftp):\/\/.+?\.(?:png|gif|jpg))\[\/img\]/igm', '<img src="$1" alt="Image">', $input);

Where $input is your page of text and $output is your result. The regular expression evaluates the url and bbcode then adds it to a tag. I have added the alt="" attribute just to make it more html valid. I have also allowed the ftp protocol as some ftp servers allow public access to image files.

The /igm makes it case insensitive, allows subsequent searches to start from the end of the previous match and will match the start and end of a line, instead of the start and end of the whole string.

Upvotes: 0

sg3s
sg3s

Reputation: 9567

Something like this:

<?php

$rgx = '/\[img\](https?:\/\/.+?\.(?:png|gif|jpg))\[\/img\]/';
$arr = array(
'sdvsvsdvsdv[img]http://www.website/folders/url_here.png[/img]sdvsdvsvdsvdsdv',
'sdvsvdsvd[img]https://www.website/folders/url_here.jpg[/img]svsnvlkn',
'sdvsdvsdv[img]https://www.website/folders/url_here.gif[/img]svdsvd'
);

$all = 'sdvsvsdvsdv[img]http://www.website/folders/url_here.png[/img]sdvsdvsvdsvdsdv
sdvsvdsvd[img]https://www.website/folders/url_here.jpg[/img]svsnvlknsdvsdvsdv[img]https://www.website/folders/url_here.gif[/img]svdsvd';

echo "Individual strings:\n";
foreach ($arr as $str) {
    echo preg_replace($rgx, '<img src="$1" alt="">', $str) . "\n";
}

echo "\nAll together: \n";
echo preg_replace($rgx, '<img src="$1" alt="">', $all) . "\n";

Codepad: http://codepad.org/n00B6akn

Some little tweaks:

Alternatively this tweak speeds up the regex I made by keeping it greedy instead of making the middle non-greedy:

$rgx = '/\[img\](https?:\/\/[^\[]*\.(?:png|gif|jpg))\[\/img\]/'

And here without as many slashes:

$rgx = '~\[img\](https?://[^\[]*\.(?:png|gif|jpg))\[/img\]~'

It makes sure the img tags are matched, and that whatever is inbetween them starts with http(s):// and ends with .png / .gif / .jpg. If you need better validation then something like what hwnd made initially (v1 of his answer) is more fitting (given you tweak his solution to your needs).

Upvotes: 1

hwnd
hwnd

Reputation: 70732

You can use the following for this.

$text = <<<DATA
[img]http://www.website/folders/url_here.png[/img]
[img]https://www.website/folders/url_here.jpg[/img]
[img]https://www.website/folders/url_here.gif[/img]
DATA;

$text = preg_replace('~\[(img)\](https?://\S+\.(?:png|gif|jpg))\[/\1\]~', 
                     '<img src="$2" />', $text);

Output

<img src="http://www.website/folders/url_here.png" />
<img src="https://www.website/folders/url_here.jpg" />
<img src="https://www.website/folders/url_here.gif" />

See Live demo

Upvotes: 1

Related Questions