Connecting Tech Pros Worldwide Forums | Help | Site Map

preg_match exclude string within subpattern

Han
Guest
 
Posts: n/a
#1: Jul 17 '05
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.

For example:

<a href="http://www.example.com><b>Example</b></a>

to return

<a href="http://www.example.com>Example</a>

How can this be accomplished?

Thanks (again) in advance.



Pedro
Guest
 
Posts: n/a
#2: Jul 17 '05

re: preg_match exclude string within subpattern


Han wrote:[color=blue]
> 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.
>
> For example:
>
><a href="http://www.example.com><b>Example</b></a>
>
> to return
>
><a href="http://www.example.com>Example</a>
>
> How can this be accomplished?[/color]

I think your best bet would be to preg_replace() the bold out:

$anchor = preg_replace('#</?b>#i', '', $anchor);


--
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.
Han
Guest
 
Posts: n/a
#3: Jul 17 '05

re: preg_match exclude string within subpattern


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.

Thanks.

"Pedro" <hexkid@hotpop.com> wrote in message
news:blu1f7$g5t2p$1@ID-203069.news.uni-berlin.de...[color=blue]
> Han wrote:[color=green]
> > 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.
> >
> > For example:
> >
> ><a href="http://www.example.com><b>Example</b></a>
> >
> > to return
> >
> ><a href="http://www.example.com>Example</a>
> >
> > How can this be accomplished?[/color]
>
> I think your best bet would be to preg_replace() the bold out:
>
> $anchor = preg_replace('#</?b>#i', '', $anchor);
>
>
> --
> 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.[/color]


Pedro
Guest
 
Posts: n/a
#4: Jul 17 '05

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.
Closed Thread