[英]PHP str_replace tags with using arrays
I have the following str_replace
code which takes a given comment string and replaces any instances with a dot followed by [++]
with the word He
with a capital letter. 我有以下str_replace
代码,该代码接受给定的注释字符串,并将所有实例替换为一个点,然后将[++]
替换为带有大写字母He
的单词[++]
。 Otherwise it replaces with he with no capital letters. 否则,将其替换为没有大写字母的他。
$comment = str_replace(array(". [++]","[++]"), array(". He","he"), $comment);
Although this works in most cases. 尽管这在大多数情况下有效。 It doesnt work if my comment string has [++]
tag in the beginning of the sentence or if the tag is after a line break or two. 如果我的注释字符串在句子的开头带有[++]
标记,或者该标记在一个或两个换行符之后,则不会起作用。 It only works if the dot is next to the tag. 仅当圆点位于标签旁边时才有效。
Any ideas how I can get it to replace with the capital He if its after a line break or the first tag in beginning of the comment string? 如果在换行符后或注释字符串开头的第一个标记之后,如何将其替换为大写的He,有什么想法吗?
Try the preg_replace()
function ( http://php.net/manual/en/function.preg-replace.php ): 尝试preg_replace()
函数( http://php.net/manual/en/function.preg-replace.php ):
$comment = preg_replace(
array("~^\[\+\+\]~", "~(\.\s+)\[\+\+\]~", "~\[\+\+\]~"),
array("He", "$1He", "he"),
$comment
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.