Problems getting PHP regular expression

Here is my problem. This is probably a simple solution. I have a regex that I use to replace BBCode. What I have now does not work like that.

<?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);
?>

Currently, the original $ input_string is being output, while I would like it to output the following.

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

What am I missing?

+3
source share
2 answers
<?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);
?>
  • On the BBCode line, I closed [url].
  • I escaped a ]in regex (not sure if this was the real problem).

Please note that this is [url]http://example.org[/url]also a valid way to link to BBCode.

You should listen to comments suggesting using the existing BBCode parser.

+3

: $regex = '/[url=(.+?)](.+?)[url]/is';

, . , . : http://pastebin.com/6pF0FEbA

0

Source: https://habr.com/ru/post/1783036/


All Articles