Regular Expression/URL Matching help needed!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
GodOfHonk
Forum Newbie
Posts: 6
Joined: Thu May 09, 2002 2:17 pm
Location: St. Louis

Regular Expression/URL Matching help needed!

Post by GodOfHonk »

Hello,

I have a regular expression that matched URLs and turns them into links. I now need to make sure this happens only if the URL is NOT enclosed in square brackets.

So the url for yahoo would be rendered as a link here:

Code: Select all

http://www.yahoo.com is a search engine
But not here:

Code: Select all

їhttp://www.yahoo.com] is a search engine

Here's the regexp I'm using so far:

Code: Select all

$Text = preg_replace("/(&#1111;\w]+:\/\/&#1111;\w-?&;#~=\.\/\@]+&#1111;\w\/])/i", "<a href="$1">$1</a>", $Text);
Any ideas? Thanks in advance!!
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Post by Takuma »

You could just use

Code: Select all

&amp;lt;?php
  if(strstr($string,"&#1111;")) {
    echo "'&#1111;' found!";
  }
?&amp;gt;
dusty
Forum Contributor
Posts: 122
Joined: Sun Apr 28, 2002 9:52 pm
Location: Portsmouth, VA

Post by dusty »

i don't see how that would help. it's only locating the [ but not replacing the urls w/o [] with a link.

Code: Select all

$text = preg_replace("/(?&lt;!\&#1111;)(http:\/\/)(&#1111;\S\.]+)\b/i","&lt;a href="http://$2$3$4"&gt;$1$2$3$4&lt;/a&gt;", $text);
should skip [http://*] and link http://*
GodOfHonk
Forum Newbie
Posts: 6
Joined: Thu May 09, 2002 2:17 pm
Location: St. Louis

Post by GodOfHonk »

Excellent!

Got it working, Thanks!!

-GodOfHonk
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Post by Takuma »

dusty wrote:i don't see how that would help. it's only locating the [ but not replacing the urls w/o [] with a link.

Code: Select all

$text = preg_replace("/(?&lt;!\&#1111;)(http:\/\/)(&#1111;\S\.]+)\b/i","&lt;a href="http://$2$3$4"&gt;$1$2$3$4&lt;/a&gt;", $text);
should skip [http://*] and link http://*
It does

Code: Select all

&lt;?php
  if(!strstr($string,"&#1111;")) { 
    $Text = preg_replace("/(&#1111;\w]+:\/\/&#1111;\w-?&amp;;#~=\.\/\@]+&#1111;\w\/])/i", "&lt;a href="$1"&gt;$1&lt;/a&gt;", $Text); 
  } else {
    return false;
  }
?&gt;
dusty
Forum Contributor
Posts: 122
Joined: Sun Apr 28, 2002 9:52 pm
Location: Portsmouth, VA

Post by dusty »

that makes no sense to me..all you're doing is finding an occurence of [ in the string.

"[ before http://www.yahoo.com" would be skipped

just makes no sense to use strstr in this situation.
Post Reply