Processing text / regexps

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

    Processing text / regexps

    I'm trying to work out how to process a block of text which contains tags
    similar to [link]http://something.com[/link]. I want to work through the
    text block and pass the contents of each tag to function and then carry on
    through the text when the function returns. I don't necessarily want to
    change the text block, I think if I did it would make it easier as then I
    could use a loop though the changing block with a regexp to find the next
    unchanged tag until it could find no matches. Can anyone suggest a method of
    doing it without changing the text block?

    Many thanks,
    Geoff


  • Chung Leong

    #2
    Re: Processing text / regexps

    "Geoff Soper" <geoff.news.nos pam@alphaworks. co.uk> wrote in message
    news:406b37ff$0 $6551$cc9e4d1f@ news-text.dial.pipex .com...[color=blue]
    > I'm trying to work out how to process a block of text which contains tags
    > similar to [link]http://something.com[/link]. I want to work through the
    > text block and pass the contents of each tag to function and then carry on
    > through the text when the function returns. I don't necessarily want to
    > change the text block, I think if I did it would make it easier as then I
    > could use a loop though the changing block with a regexp to find the next
    > unchanged tag until it could find no matches. Can anyone suggest a method[/color]
    of[color=blue]
    > doing it without changing the text block?[/color]

    Use preg_replace_ca llback(). No looping. No changing the original text.

    function TurnIntoAnchorT ag($matches) {
    $url = htmlspecialchar s($matches[1]);
    return "<a href=\"$url\">" $url"</a>";
    }

    $new_text = preg_replace_ca llback('/[link](.*?)[\/link]/i',
    'TurnIntoAnchor Tag', $text);


    Comment

    • Geoff Soper

      #3
      Re: Processing text / regexps

      "Chung Leong" <chernyshevsky@ hotmail.com> wrote in message news:<puidnQKtt L5IwvbdRVn-uw@comcast.com> ...[color=blue]
      > "Geoff Soper" <geoff.news.nos pam@alphaworks. co.uk> wrote in message
      > news:406b37ff$0 $6551$cc9e4d1f@ news-text.dial.pipex .com...[color=green]
      > > I'm trying to work out how to process a block of text which contains tags
      > > similar to [link]http://something.com[/link]. I want to work through the
      > > text block and pass the contents of each tag to function and then carry on
      > > through the text when the function returns. I don't necessarily want to
      > > change the text block, I think if I did it would make it easier as then I
      > > could use a loop though the changing block with a regexp to find the next
      > > unchanged tag until it could find no matches. Can anyone suggest a method[/color]
      > of[color=green]
      > > doing it without changing the text block?[/color]
      >
      > Use preg_replace_ca llback(). No looping. No changing the original text.
      >
      > function TurnIntoAnchorT ag($matches) {
      > $url = htmlspecialchar s($matches[1]);
      > return "<a href=\"$url\">" $url"</a>";
      > }
      >
      > $new_text = preg_replace_ca llback('/[link](.*?)[\/link]/i',
      > 'TurnIntoAnchor Tag', $text);[/color]

      Brilliant, thanks very much

      Comment

      Working...