Extract text from tags multiple times

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • beary
    New Member
    • Nov 2006
    • 170

    Extract text from tags multiple times

    Hi,

    If I have this:

    Today is a good day
    [LINK]Johnny is good[/LINK]
    Tomorrow will be better
    [LINK]Mary is bad[/LINK]
    Who cares

    And there could be anywhere between 0 and 4 occurrences of the [LINK]...some text...[/LINK] tags, how can I extract the strings

    "Johnny is good" and "Mary is bad" ?

    Really nice would be to have these stored in an array, such as

    $myarray=array( 1=>'Johnny is good',2=>'Mary is bad');

    Any pointers or ideas? Many thanks!
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    You'll almost certainly need to use a regular expression. Check out preg_match(), this code snipper on BBCode (very much like what you're doing) and also check out the 3rd parameter of preg_match() - it will store matches into an array for you.

    Comment

    • beary
      New Member
      • Nov 2006
      • 170

      #3
      Hey there Markus,

      As usual, you've been a superb help, and I really appreciate it! My actual problem was to take

      Today is a good day
      [LINK]Johnny is good[/LINK]
      Tomorrow will be better
      [LINK]Mary is bad[/LINK]
      Who cares

      and then match each of the strings inside the link tags to a record in mysql, in order to grab the actual path to that file, so I could then create the url instead of the links tags.

      So now, after looking at the pages you suggested, I have
      Code:
      function bbcode_html($input) {
      $bbcode = array(
      '/\[link=(.+?)\](.+?)\[\/link\]/i'
      );
      
      $html = array(
      '<a href="$1">$2</a>'
      );
      return preg_replace($bbcode, $html, $input);
      So, if I run a query to find my magical pathway, such as
      Code:
      echo bbcode_html('Today is a good day<br>
      [LINK=myfirstpath]Johnny is good[/LINK]<br>
      Tomorrow will be better<br>
      [LINK=mynextpath]Mary is bad[/LINK]<br>
      Who cares');
      it will now return the html

      Today is a good day
      <a href='myfirstpa th'>Johnny is good</a>
      Tomorrow will be better
      <a href='mynextpat h'>Mary is bad</a>
      Who cares

      Which is perfect.

      And I can now easily extend to using [web] tags or similar for simple url web links. It needed to be so a user could simply place tags around a web link and have it create the url for them.

      I think I better get my head around regular expressions. They seem pretty powerful. Again, thanks for the advice.

      Cheers

      Originally posted by Markus
      You'll almost certainly need to use a regular expression. Check out preg_match(), this code snipper on BBCode (very much like what you're doing) and also check out the 3rd parameter of preg_match() - it will store matches into an array for you.

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        Originally posted by beary
        Code:
        '/\[link=(.+?)\](.+?)\[\/link\]/i'
        note: you actually need not stick with / as RegEx delimiter, every char will do as long as it doesn't have a special meaning. so you can skip some of the escapes (well, your case it's only one, but there may be times it comes in handy). e.g.
        Code:
        '@\[link=(.+?)\](.+?)\[/link\]@i'

        Comment

        Working...