Page 1 of 1

Regular Expression/URL Matching help needed!

Posted: Thu Sep 12, 2002 3:03 pm
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!!

Posted: Fri Sep 13, 2002 4:43 pm
by Takuma
You could just use

Code: Select all

&amp;lt;?php
  if(strstr($string,"&#1111;")) {
    echo "'&#1111;' found!";
  }
?&amp;gt;

Posted: Sat Sep 14, 2002 1:36 am
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://*

Posted: Sun Sep 15, 2002 12:34 am
by GodOfHonk
Excellent!

Got it working, Thanks!!

-GodOfHonk

Posted: Sun Sep 15, 2002 3:14 am
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;

Posted: Sun Sep 15, 2002 10:49 am
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.