| re: preg_match exclude string within subpattern
Han top-posted:[color=blue]
> "Pedro" <hexkid@hotpop.com> wrote in message
> news:blu1f7$g5t2p$1@ID-203069.news.uni-berlin.de...[color=green]
>> Han wrote:[color=darkred]
>> > Yet another newbie regular expression question (I swear, I'm learning!)
>> >
>> > I have a simple pattern that captures an anchor tag:
>> >
>> > (<a.*?</a>)
>> >
>> > It works great. I would now like to modify this to EXCLUDE occasional
>> > opening and closing bold tags (<b></b>) around the text.[/color]
>>
>> I think your best bet would be to preg_replace() the bold out:
>>
>> $anchor = preg_replace('#</?b>#i', '', $anchor);[/color]
>
> I was exploring ways to do it in one pass, but I could easily do as you
> suggest before outputting each anchor to the page.[/color]
ok ... try this if you really want one single pass
<?php
$regex = '#(<a.*)>(?:<b>)?([^<]*)(?:</b>)?</a>#Ui';
### \_____/\______/\_____/\_______/ |`- case insensitive
### | | | | `- ungreedy
### | | | `- optional "</b>" not captured
### | | `- linked text; if .* matches <b>
### | | so anything but "<" instead :)
### | `- optional "<b>" not captured
### `- the "<a ...>" tag and its parameters
preg_match($regex, $text, $results);
echo $results[1], $results[2], '</a>';
?>
--
I have a spam filter working.
To mail me include "urkxvq" (with or without the quotes)
in the subject line, or your mail will be ruthlessly discarded. |