How does stripos and str_replace work?

225 Views Asked by At

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!

2

There are 2 best solutions below

0
On

Use str_replace...

str_replace("Pattern to search",$stringToSearch,"Replacement text");

So in practice:

$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;

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

$entity->getBodyOfText(); 

replace $string with that, OR assign

$string = $entity->getBodyOfText();
3
On

Using a non-regex solution requires that you know exactly the substring you are trying to replace -- I'll assume you know the substring. A word of caution, if there is any chance of the substring occurring more than once and you only want one replacement, then str_replace() will fail you by replacing all found substrings. If the substring will be unique in the string, or you would like to replace all duplicate substrings, then everything will work as intended.

Code (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";

Output:

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 you need a better tailored solution, please explain how this method is failing you and I'll adjust it.