Re: Reg Expression
| From: | Daniel Convissor | Date: | Tue, 19 Dec 2000 07:06:18 +0000 |
| Subject: | Re: Reg Expression | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-31003@lists.php.net to get a copy of this message | ||
Hi Scott:
> Hi guys I have some html text and I am trying to find all instances of '<a
> href="*">' and replace it with [link | *]
> ...
> I can put it back into
> '<a href="*">'. * will could contain a-z, A-Z, 0-9 and/or #.
That's not accurate. URL's can contain a wide variety of characters. Here's
some code I use to turn URL's into hyperlinks:
eregi_replace("(http://|https://|ftp://|gopher://|news:|mailto:)
([[:alnum:]/!#$%&'()*+,.:;=?@~-]+)([[:alnum:]/!#$%&'()*+:;=?@~-])",
"<a href=\"\\1\\2\\3\">\\1\\2\\3</a>", "$Val");
Performs the following, for example:
http://foo.org
<a href="http://foo.org">foo.org</a>
> $content = ereg_replace("<\/a href=\"(.+)\">", "[link]",
> $content);
You'll get tripped up by that expression because the atom "(.+)" will grab
everything up to the last ">" in the string. So, if you have several links
in the string, you'll cram all the text between the first "<a href" to the
last ">" into one link. Not what you're looking for. So, use
"([[:alnum:]/!#$%&'()*+,.:;=?@~-]+)" for it instead.
Also, use eregi to ensure the thing works if someone has text in upper case.
Uh, why do you have "<\/a href" rather than "<a href" ???
Anyway, here's a quick, untested stab at what you want:
$content = eregi_replace("(<a href=\")
([[:alnum:]/!#$%&'()*+,.:;=?@~-]+)(\">)",
"[link | \\2]", "$content");
Good luck,
--Dan
--
PHP scripts that make your job easier
http://www.analysisandsolutions.com/code/
SQL Solution | Layout Solution | Form Solution
T H E A N A L Y S I S A N D S O L U T I O N S C O M P A N Y
4015 7 Ave, Brooklyn NY 11232 v: 718-854-0335 f: 718-854-0409