开发者

Having problems getting a PHP regex to match

Here is my problem. It's probably a simple fix. I have a regex that I am using to replace a url BBCode. What I have right now that is not working looks like this.

<?php
$input_string = '[url=www.test.com]Test[url]';
$regex = '/\[url=(.+?)](.+?)\[\/url]/is';
$replacement_string = '<a href="$1">$2</a>';
echo preg_replace($regex, $replacement_string, $inp开发者_开发知识库ut_string);
?>

This currently outputs the original $input_string, while I would like it to output the following.

<a href="www.test.com">Test</a>

What am I missing?


<?php
$input_string = '[url=www.test.com]Test[/url]';
$regex = '/\[url=(.+?)\](.+?)\[\/url\]/is';
$replacement_string = '<a href="$1">$2</a>';
echo preg_replace($regex, $replacement_string, $input_string);
?>
  • In your BBCode string, I closed the [url] properly.
  • I escaped a ] in the regex (not sure if that was an actual problem).

Note that [url]http://example.org[/url] is also a valid way to make a link in BBCode.

You should listen to the comments suggesting you use an existing BBCode parser.


Change this line as follows: $regex = '/[url=(.+?)](.+?)[url]/is';

OK, the formatting is not proper. While I figure it out, see this: http://pastebin.com/6pF0FEbA

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜