Hi all, this is my first post here.

At the moment I am trying to use php to perform what should be a simple action, but it is failing and driving me up the wall.

I have a script which is able to pull a specified number of posts from a phpbb forum (phpbb version 3.05 - latest version), and then display those posts into a php page. The problem I have is that it stripped all the BBCode tags out, and I need URL links, and embedded images to show at the very least. I took out the line of script stripping back the BBCode tags - but here is where I'm getting stuck.
I need to replace the BBCode "[img]" with the HTML equivalent:

Example to replace

[ img ]http://www.google.co.uk/images/nav_logo7.png[ /img ]

(note I put spaces into the IMG tags above so they'd show)

Desired output

<img src="www.google.co.uk/images/nav_logo7.png">

But I encounter a problem in that every single bbcode tag has a string of random numbers after it, like this

[IMG:2j4355]www.google.co.uk/images/nav_logo7.png[/IMG:2j4355]
[url:1y9jihpq]http://www.google.co.uk[/url:1y9jihpq]

I can't use a direct str_replace because of the random jibberish that appears after these bbcode tags, but I need some sort of wildcard to get rid of this.

essentially, looking like this
str_replace("[img:WILDCARD]",'<img src="', $string);
str_replace("[/img:WILDCARD]", '" />", $string);

However I believe that wildcards can't be used in str_replace, and I've tried using preg_replace and it replaces everything inside the [ ] brackets (so if I'm trying to replace a [ URL ] then every single U, R, or L becomes [ URL ]

I need the images and url links to work, as I plan to use this script on a homepage so it'll automatically update when a particular forum thread is updated... so any help is appreciated.

    Since [ and ] have a special meaning in PCRE regular expressions, if you need them to be treated as literal '[' and ']' characters you need to escape them, i.e., write them in the expression as [ and ].

      Escaping [ is sufficient. ] only ends a character class if one was started with [ in the first place. Minor difference, but reducing \ makes it easier to read patterns imo.

        Well I can that sort of working..

        ereg_replace('🙁.*)]',']',$message_text);

        by using the above I am trying to make it so a bbcode tag like [ quote="somebloke":345tr664 ] takes off everything after the : and closes the tag. However, instead it goes until the end of the quote and replaces the whole block.

        so, what was [ quote="somebloke":345tr664 ]my name is bob[ /quote:345tr664 ] becomes [ quote="somebloke" ] and that's it.... and I don't know how to stop that from happening.

        Also is there any way of a double wildcard within the syntax? simply because I want to be able to change the bbcode example [ quote="somebloke":345tr664 ]my name is bob[ /quote:345tr664 ] into

        <b>Somebloke wrote</b><br><i>my name is bob</i>

        And likewise with URL's - I'd like the URL value and the actual text linking to the url to be retained. example:
        [ url=http://www.google.co.uk/page.htm:1hkvl53s ]test page text[ /url:1hkvl53s ]
        I'd like the www.google.co.uk/page.htm to remain, but to get rid of that 1hkvl53s jibberish at the end of the tag. I figure I could use two seperate ereg_replace statements so as to keep the text in the middle that forms the clickable link, but essentially I need a statement like

        ereg_replace('[url=WILDCARD1:WILDCARD2]','<a href="WILDCARD1" target="window">',$message_text);

        But I don't know whether that's possible or not.

          Well, I tried a preg_replace and all it did was replace everything inside the [ ] brackets, and if I slashed off the squared brackets, it didn't do anything.

          all I want to do is to be able to change the phpbb code into plain html so that
          [img:3dhzgqpe]http://www.google.co.uk/images/logo7.png[/img:3dhzgqpe] changes to <img src="http://www.google.co.uk/images/logo7.png">

          if php can get rid of the : and trailing random characters at the end of each bb tag then I can program the rest in (like getting rid of the ":3dhzgqpe" from above example) but at the moment this is doing my head in.

          And I can't get my head around regex either! :rolleyes:
          I guess if anybody can modify the below code so that bbcode IMG tags can be converted to html IMG tags then I'd appreciate it, as I'll then be able to see what to do!

          <?php
          $input = "plain [indent] deep [indent] deeper [/indent] deep [/indent] plain";
          
          function parseTagsRecursive($input)
          {
          
          $regex = '#\[indent]((?:[^[]|\[(?!/?indent])|(?R))+)\[/indent]#';
          
          if (is_array($input)) {
              $input = '<div style="margin-left: 10px">'.$input[1].'</div>';
          }
          
          return preg_replace_callback($regex, 'parseTagsRecursive', $input);
          }
          
          $output = parseTagsRecursive($input);
          
          echo $output;
          ?>

            sorry, bad form here double posting, but the code parsing on this forum messed up the script and I can't see an 'edit post' button...

            right, take two. if anybody can modify this script so that the bbcode IMG tags like in my above post (with : and trailing garbage) can be turned into html IMG tags then I'd appreciate it

            (i've put a space after each [ and before & after the word Indent)

            <?php
            $input = "plain [ indent ] deep [ indent ] deeper [ /indent ] deep [ /indent ] plain";
            
            function parseTagsRecursive($input)
            {
            
            $regex = '#\[ indent ]((?:[^[]|\[ (?!/?indent ])|(?R))+)\[ /indent ]#';
            
            if (is_array($input)) {
                $input = '<div style="margin-left: 10px">'.$input[1].'</div>';
            }
            
            return preg_replace_callback($regex, 'parseTagsRecursive', $input);
            }
            
            $output = parseTagsRecursive($input);
            
            echo $output;
            ?>

              You might have better luck (and like the result better) using this forum's [noparse]

              ...

              tags instead of the generic

              ...

              [/noparse] tags.

              On a more subject-related note, I don't know if you'd want to look into it at this stage of your project, but there is a [man]BBCode[/man] PECL extension, or you could also look into the PEAR HTML_BBCodeParser package.

                Write a Reply...