I am struggling to understand how the php functions stripos
and str_replace
work.
I have a body of text such as : {% if group.newt !== "" %} XYZ's {% else %} ABC's {% endif %}
and am wanting to replace that text with Go to this link www.google.com
.
I search through the bodies of text:
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());
The results I am getting are that the text is not being found or replaced! I do not understand why. Could someone please shed light on this for me?
EDIT:
The body of text is an email template with lots of images, text, and twig code. In a particular set of email templates, I need to find and replace a whole block of twig code with one line of text (it doesn't matter what that text is). The problem I'm having is when I search for the block of code in the email templates using str_replace
or preg_replace
, those functions do not find or replace the block I am trying to find and replace.
So my output is the same (nothing is found, nothing is changed).
For example:
`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`
I hope that helps!
Use str_replace...
So in practice:
Fyi, you'll need to href that link for it to be an actual link though. Also fixed your "" in your comparison to be '' to accommodate PHP encasing with " ";
Tested in PhpFiddle.com
If you plan to use your function
replace $string with that, OR assign