【发布时间】:2017-02-25 11:17:06
【问题描述】:
我正在寻找用户可以在 textarea 内容框中添加 [name] 短代码的功能。所以不执行它将用从数据库中获取的数据替换短代码,即John Doe。
[姓名] = John Doe
[电子邮件] = johny@gmail.com
[电话] = 9876543210
假设用户输入源代码为<p>my name is [name]</p>。
因此提交页面短代码应替换为<p>my name is John Doe</p>。
我尝试了下面的代码,但它的短代码很长,打开/关闭和中间的内容很难让客户记住。
$text = "My example text [shortcode_name]content of shortcode[/shortcode_name] is cool.";
$text = "My example text [shortcode_xyz]content of shortcode[/shortcode_name] is cool.";
$bhaku = "ss";
$pattern= '/\[(shortcode_name)\](.|\s)*?\[\/\1\]/';
echo preg_replace($pattern,$bhaku ,$text);
【问题讨论】:
-
在这种情况下您不需要使用
str_replace吗?也就是说,如果您只想替换[shortcode]而不是[shortcode]Foo bar[/shortcode]