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.

8
  • 1
    Can you post some of the text your are trying to replace and what you'd like the output to be? Commented Sep 25, 2011 at 13:56
  • 4
    One advice, don't do it this way - you can get something like "<b>lorem<i>ipsum</b>ipsum</i>", 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. Commented Sep 25, 2011 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. Commented Sep 25, 2011 at 14:06
  • possible duplicate of Best way to parse bbcode Commented Sep 25, 2011 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.) Commented Sep 25, 2011 at 14:13

2 Answers 2

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:

Sign up to request clarification or add additional context in comments.

Comments

1

Try ~ as a delimiter instead

preg_match("~<b>(.*?)</b>~is", $text, $b);

1 Comment

From the docs: A delimiter can be any non-alphanumeric, non-backslash, non-whitespace character. So ' is perfectly valid as a delimiter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.