agh - what am I doing wrong - regular expressions
| From: | sgibbs at vt dot globalhealth dot org | Date: | Sun, 20 Jan 2002 15:45:37 +0000 |
| Subject: | agh - what am I doing wrong - regular expressions | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-81270@lists.php.net to get a copy of this message | ||
Good morning everyone,
I'm trying to adapt existing php code that uses a while loop to scan content
for a url pattern, take the matches and add link tags (<a>) around them so
the url with be made into "hot links. Simple enough right? Nevertheless, I
seem to be having trouble with urls that have question marks in them.
Here is the existing code that works - except with question marks:
********************
$content = "http://www.globalhealth.org/text.php3?id=151
adjfladjfajdfkladfjl;kadjf
jlkadjflkajdflkj jadklfjalkdjfl;df jalkdfj;ldafj";
$pattern = "http://([\/~_\.0-9A-Za-z#&=-]+)";
while(ereg($pattern, $content, $match)){
$http_old = $match[0];
$http = "dubdubdub" . $match[1] . $match[2];
$url = "<A
HREF=\"javascript:externalURL('$http')\";>$http</A>";
$content = ereg_replace($http_old, $url, $content);
};
$content = ereg_replace("dubdubdub", "http://",
$content);
print $content;
******************
Here's the same code with the question mark in the pattern variable. When I
add the question mark and test the page, my browsers just "hang". Escaping
the question mark doesn't seem to help (and I also read that escapes aren't
necessary between square brackets]:
$content = "http://www.globalhealth.org/text.php3?id=151
adjfladjfajdfkladfjl;kadjf
jlkadjflkajdflkj jadklfjalkdjfl;df jalkdfj;ldafj";
$pattern = "http://([\/~_\.0-9A-Za-z#&=?-]+)";
while(ereg($pattern, $content, $match)){
$http_old = $match[0];
$http = "dubdubdub" . $match[1] . $match[2];
$url = "<A
HREF=\"javascript:externalURL('$http')\";>$http</A>";
$content = ereg_replace($http_old, $url, $content);
};
$content = ereg_replace("dubdubdub", "http://",
$content);
print $content;
*************************
In an attempt to find the problem, I did a test with the following code and
received the result I want - which leads me to believe the problem is
possibly with the while loop or the ereg_replace??
$content = "http://www.globalhealth.org/text.php3?id=151
adjfladjfajdfkladfjl;kadjf
jlkadjflkajdflkj jadklfjalkdjfl;df jalkdfj;ldafj";
$pattern = "http://([/~_.0-9A-Za-z#&=?-]+)";
ereg($pattern, $content, $match);
print "<A
HREF=\"javascript:externalURL('$match[0]')\";>$match[0]</A>";
******************
I'm completely confused. I have a feeling there is any easy answer and if I
wasn't so frustrated I'd be embarrassed to say that I've spent hours on
this...
Thanks in advance for your help, Shawna