2017-05-11 39 views
-1

我很努力去理解php函数striposstr_replace是如何工作的。stripos和str_replace如何工作?

我有文字的身体如:{% if group.newt !== "" %} XYZ's {% else %} ABC's {% endif %}

和我想与Go to this link www.google.com替换该文本。

我通过文字的机构搜索:

if(stripos($entity->getBodyOfText(), $strTFind) !== false) {preg_match("{% if group.newt !== "" %} XYZ's {% else %} ABC's {% endif %}", $strToReplace)};

OR

$str_replace($strToFind, $strToReplace, $entity->getBodyOfText());

我得到是文本没有被发现或更换的结果!我不懂为什么。有人能帮我解释一下吗?

编辑:

文本的主体是有很多图片,文本和树枝代码的电子邮件模板。在一组特定的电子邮件模板中,我需要用一行文本来查找和替换整个代码段(文本的内容无关紧要)。我遇到的问题是当我使用str_replacepreg_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` 

我希望帮助!

+0

什么是在其中有{%...%}'子字符串的完整字符串的小例子? – mickmackusa

+0

它是枝条代码...“{%if statements!==”“%}这应该显示{%else%}另一件事显示{%endif%}”。 – grgre

+0

我不熟悉枝条代码。当我尝试用str/preg替换类型问题来帮助其他用户时,我希望能够清楚地了解完整输入的内容,它们作为子字符串的目标以及取而代之的内容。我看到你想要链接字符串,但其余的不清楚。请在文本之前和之后显示完整内容,以便您的问题很清楚。除了树枝表达外还有更多文字吗? – mickmackusa

回答

0

使用str_replace函数...

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 = $entity->getBodyOfText(); 
0

使用非正则表达式的解决方案要求你确切知道你想要替换的子串 - 我假设你知道子串。请注意,如果子字符串有多次出现的机会,而您只需要一次替换,那么str_replace()将通过替换所有找到的子字符串而失败。如果子字符串在字符串中是唯一的,或者您想要替换所有重复的子字符串,那么一切将按预期工作。

码(Demo):

$find='{% 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 %}.'; 
$replace='LINK'; 

$text=str_replace($find,$replace,$text); 
echo "$text"; 

输出:

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 

如果你需要一个更好的定制解决方案,请解释一下怎样用这种方法失败你,我会调整。

+0

@grgre请给我一些关于我的两次尝试的反馈。如果我仍然错了,请再次尝试向我解释你的问题。如果我的模式没有充分适应所有可能的分支代码语法,请提供一个额外的输入样本供我测试。如果这些方法中的任何一个都是您正在查找的内容,请告诉我您要使用哪一个,以便我可以删除另一个。 – mickmackusa

+0

请检查我的编辑,我希望这会帮助你更好地理解。你的答案现在不适合我。 – grgre

+0

@grge我更新了我的答案。如果这无济于事,请再发表评论。 – mickmackusa