PHP Preg_match单个字符串中的多个项


PHP Preg_match multiple items in a single string?

我目前正在向数据库中插入一些正文。这只是纯文本,但用户也希望能够添加工作链接。为了避免进行大规模更改,我正在使用一个弹出窗口进行这些更改,该弹出窗口将以以下格式插入链接:

[a]http://www.google.com[/a]

每个正文文本在中可以有多个链接

当这显示在相关网页上时,链接将更改为标准html格式:

<a href="http://google.com">http://www.google.com</a>

我似乎无法设置preg_match来在字符串中多次执行此操作(见下面的示例):

hello world [a]http://google.com[/a] how are you?
Ok. [a]http://yahoo.com[/a] Thanks for asing. [a]http://bing.com[/a]

任何帮助都将不胜感激!!!

谢谢,凯恩

使用此:

$str = "[a]http://www.google.com[/a] xy [a]http://www.google.com[/a]";
$str = preg_replace("/'[a'](.*)'['/a']/Usi", "<a href='"''1'">''1</a>", $str);
echo $str;

输出:

<a href="http://www.google.com">http://​www.google.com</a> xy <a href="http://www.google.com">http://​www.google.com</a>;

这应该有效:

$str = preg_replace('~'[a](.*?)'[/a]~si', "<a href='$1'>$1</a>", $str);