It Worked! Re: agh - what am I doing wrong - regular expressions
| From: | sgibbs at vt dot globalhealth dot org | Date: | Mon, 21 Jan 2002 15:25:54 +0000 |
| Subject: | It Worked! Re: agh - what am I doing wrong - regular expressions | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-81371@lists.php.net to get a copy of this message | ||
Dear Martin, it worked!! Thank you SO much! - Shawna
-----Original Message-----
From: Martin Towell
To: 'sgibbs@vt.globalhealth.org'; php-general@lists.php.net
Sent: 1/20/02 10:25 PM
Subject: RE: [PHP] agh - what am I doing wrong - regular expressions
try this...
I changed
1. the double quotes in "$pattern = ..." to single quotes
2. escaped the "?"
3. used ereg_replace to do everything, dropping the while loop
------------------------
$content = " http://www.globalhealth.org/text.php3?id=151
<http://www.globalhealth.org/text.php3?id=151>
adjfladjfajdfkladfjl;kadjf
jlkadjflkajdflkj jadklfjalkdjfl;df jalkdfj;ldafj";
$pattern = ' http://( <http://(>
[\/~_\.0-9A-Za-z#&=-\?]+)';
$content = ereg_replace($pattern, "<A
HREF=\"javascript:externalURL('\\1');\">\\1</A>", $content);
print $content;
------------------------
-----Original Message-----
From: sgibbs@vt.globalhealth.org [ mailto:sgibbs@vt.globalhealth.org
<mailto:sgibbs@vt.globalhealth.org> ]
Sent: Monday, January 21, 2002 2:46 AM
To: php-general@lists.php.net
Subject: [PHP] agh - what am I doing wrong - regular expressions
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
<http://www.globalhealth.org/text.php3?id=151>
adjfladjfajdfkladfjl;kadjf
jlkadjflkajdflkj jadklfjalkdjfl;df jalkdfj;ldafj";
$pattern = " http://( <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:// <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
<http://www.globalhealth.org/text.php3?id=151>
adjfladjfajdfkladfjl;kadjf
jlkadjflkajdflkj jadklfjalkdjfl;df jalkdfj;ldafj";
$pattern = " http://( <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:// <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
<http://www.globalhealth.org/text.php3?id=151>
adjfladjfajdfkladfjl;kadjf
jlkadjflkajdflkj jadklfjalkdjfl;df jalkdfj;ldafj";
$pattern = " http://( <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
--
PHP General Mailing List ( http://www.php.net/ <http://www.php.net/> )
To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net
For additional commands, e-mail: php-general-help@lists.php.net
To contact the list administrators, e-mail: php-list-admin@lists.php.net