preg_replace help
| From: | Gaylen Fraley | Date: | Sat, 12 Jan 2002 03:20:22 +0000 |
| Subject: | preg_replace help | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-80260@lists.php.net to get a copy of this message | ||
I have need to be able to replace text within a string to turn it into a
link. In other words, I might have a phrase lik:
"blah, blah, blah, please visit my site at www.mysite.com. blah,
blah,blah".
I need to have the string converted to "blah, blah, blah, please visit my
site at http://www.mysite.com. blah, blah,blah". The link
might have more
than 3 sections and won't necessarily end in .com (.net,.com.nl, etc.).
Here is what I have been testing (then I get stuck):
$myLink = "Please visit my web site at www.mysite.com. You will be glad you
did!";
$repLink = preg_replace("/(www.)/i","<a href=http://\\1",$myLink);
which yields
Please visit my web site at <a href=http://www.mysite.com.
You will be glad
you did!
What I would need is
Please visit my web site at <a
href=http://www.mysite.com>www.mysite.com</a>. You will
be glad you did!
To be truthful, pattern matching is giving me migraines! Programming has
always been natural to me (32 years of it), but this challenge (eregi,
preg_match,etc.) is making me crazy! Can someone recommend a good tutorial
or book on this subject?
Thanks!