Php stripos和str_如何替代工作?
我正在努力理解php函数Php stripos和str_如何替代工作?,php,str-replace,stripos,Php,Str Replace,Stripos,我正在努力理解php函数stripos和stru-replace是如何工作的 我有一个文本体,例如:{%if group.newt!==“”%}XYZ的{%else%}ABC的{%endif%} 我想用替换该文本,请访问此链接www.google.com 我在正文中搜索: if(stripos($entity->getBodyOfText(),$strTFind)!=false) {preg_match(“{%if group.newt!=”%}XYZ的{%else%}ABC的{%endif%}
stripos
和stru-replace
是如何工作的
我有一个文本体,例如:{%if group.newt!==“”%}XYZ的{%else%}ABC的{%endif%}
我想用替换该文本,请访问此链接www.google.com
我在正文中搜索:
if(stripos($entity->getBodyOfText(),$strTFind)!=false)
{preg_match(“{%if group.newt!=”%}XYZ的{%else%}ABC的{%endif%}”,$strToReplace)}代码>
或
$str_replace($strToFind,$strToReplace,$entity->getBodyOfText())代码>
我得到的结果是没有找到或替换文本!我不明白为什么。有人能帮我解释一下吗
编辑:
正文是一个包含大量图像、文本和细枝代码的电子邮件模板。在一组特定的电子邮件模板中,我需要找到一整段细枝代码,并用一行文本替换它(不管文本是什么)。我遇到的问题是,当我使用str_replace
或preg_replace
在电子邮件模板中搜索代码块时,这些函数不会找到或替换我试图查找和替换的代码块
所以我的输出是相同的(没有发现任何东西,也没有改变任何东西)
例如:
`here would be an image
now starts a heading,
some more text with {{ twig.variable }} and then more text.
more
text, lots more text some {% twig.fucntions %}blah{% ending %} and
then here is the block
I want to find and replace: {% replace this whole thing including the brackets and percentage signs %}keep replacing
{% else %}
replace that else (everything including the brackets and percentage signs)and
{% this too %}.
some more ending text.
image,
the end`
我希望这有帮助 使用str\u替换
str_replace("Pattern to search",$stringToSearch,"Replacement text");
因此,在实践中:
$string = "{% if group.newt !== '' %} XYZ's {% else %} ABC's {% endif %}";
$newString = str_replace("{% if group.newt !== '' %} XYZ's {% else %} ABC's {% endif %}",$string,"Go to this link www.google.com");
echo $newString;
仅供参考,你将需要href链接为它是一个实际的链接虽然。还将比较中的“”固定为“”,以适应用“”封装的PHP
在PhpFiddle.com上测试
如果你打算使用你的功能
$entity->getBodyOfText();
将$string替换为该字符串,或分配
$string = $entity->getBodyOfText();
使用非正则表达式解决方案要求您确切地知道要替换的子字符串——我假设您知道该子字符串。需要注意的是,如果子字符串可能出现多次,并且您只需要一次替换,那么str_replace()
将替换所有找到的子字符串,从而使您失败。如果子字符串在字符串中是唯一的,或者您希望替换所有重复的子字符串,那么一切都将按预期工作
代码():
输出:
here would be an image
now starts a heading,
some more text with {{ twig.variable }} and then more text.
more
text, lots more text some {% twig.fucntions %}blah{% ending %} and
then here is the block
I want to find and replace: LINK
some more ending text.
image,
the end
如果您需要更好的定制解决方案,请解释此方法如何使您失败,我将对其进行调整。完整字符串中包含{%…%}
子字符串的一个小示例是什么?它是细枝代码…{%If语句!==“%}这应该显示{%else%}另一个显示{%endif%}。我不熟悉细枝代码。当我试图用str/preg替换类型问题帮助其他用户时,我希望看到完整输入的清晰解释,他们作为子字符串的目标是什么,以及取而代之的是什么。我看到您希望链接字符串进入,但其余内容尚不清楚。请在课文前后展示全文,这样你的问题就非常清楚了。twig表达式之外还有其他文本吗?@grgre请给我一些关于我两次尝试的反馈。如果我仍然大错特错,请再次向我解释你的问题。如果我的模式不能充分容纳所有可能的细枝代码语法,请提供额外的输入示例供我测试。如果这两种方法中的任何一种都是您想要的,请告诉我您想要使用哪一种,以便我可以删除另一种。请检查我的编辑,我希望这将帮助您更好地理解。您现在的答案对我不起作用。@grge我更新了我的答案。如果这没有帮助,请再次给我留言。
here would be an image
now starts a heading,
some more text with {{ twig.variable }} and then more text.
more
text, lots more text some {% twig.fucntions %}blah{% ending %} and
then here is the block
I want to find and replace: LINK
some more ending text.
image,
the end