0

我目前正在使用:

$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;

这允许我的用户输入

" 一些文本 [map] 完整地址 [/map] 一些文本一些更多文本 [map] 完整地址 2 [/map]"

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

我现在要做的是更改“单击此处查看地图”,以便改为显示“完整地址”。(用户在 [map][/map] 标签之间放置的任何文本)

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

有什么建议还是我接近这个错误?

提前致谢

4

1 回答 1

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

于 2013-09-27T07:36:25.140 回答