Jump to content

Regular expression


tmh766

Recommended Posts

Hi everyone

 

I have been trying for hours to understand a way to do this.

 

I need to replace all instances of "http" all the way to the first space character (get a url), as long as it does not end in ".jpg",".gif"...etc

 

I can match a URL,

/(http[^\s]+)/i

but cannot figure out a way to not match it if a picture extension is present in the end.

 

Thank you!

Link to comment
https://forums.phpfreaks.com/topic/191200-regular-expression/
Share on other sites

try this

$HTML = "test http:\\www.domain.comj\blar.html ing
http:\\www.domain.com\test\blar.jpg
http:\\www.domain.com\test\blar.html
http:\\www.domain.com\blar.html
http:\\www.domain.com\blar.gif
http:\\www.domain.com\blar.php
http:\\www.domain.com\blar.png
http:\\www.domain.com\blar.html
http:\\www.domain.com\blar.jpg";
$HTML = preg_replace('/\bhttp:\\\\\S+?\\\\[^.]*\.(jpg|png|gif)\S*\b/i', '', $HTML );
echo $HTML;

 

 

Link to comment
https://forums.phpfreaks.com/topic/191200-regular-expression/#findComment-1008145
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.