0
  "'\[b\](.*?)\[/b\]'is",

Is my current RegEx working. But I want to change the [] to be <> instead. But it doesn't work... What more then just the [] do I need to change.

hakre
  • 193,403
  • 52
  • 435
  • 836
John
  • 2,900
  • 8
  • 36
  • 65
  • 1
    Can you post some of the text your are trying to replace and what you'd like the output to be? – nachito Sep 25 '11 at 13:56
  • 4
    One advice, don't do it this way - you can get something like "loremipsumipsum", if you parse multiple tags. Don't use regexp to parse BBCode or anything else that is a markup language - parse it char by char or you will get invalid results. – Griwes Sep 25 '11 at 13:59
  • +1 to @Griwes comment. There are myriad solutions (including PECL and PEAR libraries) that will do the job for you, with much more success than hacking about with regex. – lonesomeday Sep 25 '11 at 14:06
  • possible duplicate of [Best way to parse bbcode](http://stackoverflow.com/questions/488963/best-way-to-parse-bbcode) – Gordon Sep 25 '11 at 14:13
  • If you're going to use a posting pseudolanguage, you should be tokenizing the data instead of storing raw HTML. (Basically, you store it in a half-parsed state.) – damianb Sep 25 '11 at 14:13
  • Finding a simple lib to parse BBcode is as easy as type it in google. You could check my implementation of template parser - it would require few changes to fit bbcode, but you can learn something from it. There _are_ code duplicates, but it is old forgotten project ;) [Grab it here](http://code.google.com/p/qfw/source/browse/trunk/php/lib/templates/template.class.php) – Griwes Sep 25 '11 at 14:14
  • @Griwes Regexes parse char by char. He just doing it wrong. But yes, a pattern that validates/parses valid markup will correctly fail to do so on invalid markup. – tchrist Nov 13 '11 at 16:43
  • @tchrist, I don't get your point here, in: first, reviving old question, second, saying that I am right. – Griwes Nov 13 '11 at 18:36

2 Answers2

1

Try ~ as a delimiter instead

preg_match("~<b>(.*?)</b>~is", $text, $b);
NikiC
  • 100,734
  • 37
  • 191
  • 225
genesis
  • 50,477
  • 20
  • 96
  • 125
  • From the [docs](http://www.php.net/manual/en/regexp.reference.delimiters.php): A delimiter can be any non-alphanumeric, non-backslash, non-whitespace character. So `'` is perfectly valid as a delimiter. – Decent Dabbler Sep 25 '11 at 14:01
1

There are various BBCode parsers available for PHP, for instance

which allows you to simply define your replacement rules by hand:

echo bbcode_parse(
    bbcode_create(
        array(
            'b' => array(
                'type'      => BBCODE_TYPE_NOARG,
                'open_tag'  => '<b>',
                'close_tag' => '</b>'
            )
        )
    ),
    '[b]Bold Text[/b]'
);
// prints <b>Bold Text</b>

Also check the various similar questions about BBCode Parsers:

Community
  • 1
  • 1
Gordon
  • 312,688
  • 75
  • 539
  • 559