与打开和关闭的HTML标签
问题描述:
更换引号我有以下字符串:与打开和关闭的HTML标签
'something can go here' and there some more text 'then some more here'
,我想成为:
<pre>something can go here</pre> and there some more text <pre>then some more here</pre>
我知道你可以这样做:
str_replace($replace, $with, $from);
但是,由于两端都是不可行的标志,最好的解决方法是什么?
答
这应该对你罚款:
(只需使用preg_replace()
,然后根据你想你可以替换引号)
<?php
$str = "'something can go here' and there some more text 'then some more here'";
echo $str = preg_replace("/'(.*?)'/", "<pre>$1</pre>", $str);
?>
输出:
<pre>something can go here</pre> and there some more text <pre>then some more here</pre>
//^^^ ^^^^^^ ^^^^^ ^^^^^^
您的字符串包含 ''也在两个地方。你只是通过把它们放在''中来强调这两个部分。而且总是你的字符串就像这样或者单词和句子会改变吗? – 2015-04-04 15:26:57