字符串上的PHP短代码

时间:2013-09-27 07:20:15

标签: php string google-maps str-replace explode

我目前正在使用:

$string = str_replace("[map]","<a target=\"_new\" href=\"https://maps.google.com/?q=",
$string);
$string = str_replace("[/map]","\">Click Here to View the Map</a>",$string);
echo $string;

这允许我的用户输入

  

“一些文字[地图]完整地址[/地图]一些文字更多文字[地图]完整地址编号2 [/地图]”

它会自动为其创建谷歌地图链接。

我现在要做的是更改“点击此处查看地图”,以便显示“完整地址”。 (无论用户在[地图] [/地图]标签之间放置什么文字)

如果$ string中没有多个[map]项目,这将很容易。

有任何建议或我是否接近这个错误?

提前致谢

1 个答案:

答案 0 :(得分:2)

然后我会使用正则表达式:

$string = 'text [map]link[/map] and another text';
$string = preg_replace('/(\[map\])(.*?)(\[\/map\])/','<a target="_new" href="https://maps.google.com/?q=$2">$2</a>',$string);
乍一看,它们似乎有点复杂,但在这种情况下非常有用。如果您有兴趣,可以进一步阅读:http://php.net/manual/de/function.preg-replace.php