Re: preg_replace help
| From: | Jackson Miller | Date: | Tue, 03 Jun 2003 17:18:54 +0000 |
| Subject: | Re: preg_replace help | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-150024@lists.php.net to get a copy of this message | ||
Maybe I should clarify.
$message = '<p>Visit <a href="http://jaxn.org" target="_top">My personal
site</a> to learn more about me.</p>
<p>Visit <a href="http://memigo.com">memigi</a> to learn about
current
events.';
What I want is to run a preg_replace to get the following:
Visit My personal site (http://jaxn.org) to learn more about me.
Visit memigo (http://memigo.com) to learn about current events.
stripping the HTML is easy, but before I do I want to convert the links.
Thanks,
-Jackson
On Tue, 2003-06-03 at 11:59, Ernest E Vogelsinger wrote:
> At 18:43 03.06.2003, Jackson Miller said:
> --------------------[snip]--------------------
> >I am trying to create a text only version of an HTML formatted message.
> >I would like to convert all links from:
> ><a href="http://domain.tld.page.ext">link
> >name</a>
> >to:
> >link name [http://domain.tld/page.ext]
> >
> >The problem is that some links may have additional modifiers and onthers
> >may not.
> >
> >I have seen many examples of doing the reverse, but I can't seem to find
> >an example of this.
> >
> >If anyone has done something similar and would like to share, please do.
> --------------------[snip]--------------------
>
> Sorry - you want the enclosed "link name"
> This should work:
>
> $result = null;
> while
>
> (preg_match('!(.*)<.*?a.*?href\s*=\s*"(.*?)".*?>(.*?)<\s*/\s*a\s*>(.*)!i',
> $html, $aresult)) {
> $result .= $aresult[1];
> if ($aresult[2] && $aresult[3]) $result .= $aresult[3] . ' [' .
> $aresult[2] . ']';
> $html = $aresult[4];
> }
> $result .= $html;
>
>
> --
> >O Ernest E. Vogelsinger
> (\) ICQ #13394035
> ^ http://www.vogelsinger.at/
>
>