preg_match exclude string within subpattern

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Han

    preg_match exclude string within subpattern

    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

    #2
    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.

    Comment

    • Han

      #3
      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$g5t 2p$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]


      Comment

      • Pedro

        #4
        Re: preg_match exclude string within subpattern

        Han top-posted:[color=blue]
        > "Pedro" <hexkid@hotpop. com> wrote in message
        > news:blu1f7$g5t 2p$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($reg ex, $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.

        Comment

        Working...