php


How does stripos and str_replace work?


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...
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();
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.

Related Links

What file should I edit to change the wordpress blog post list
Should I create different SQL tables for similar usages or create just one with a parameter?
Preg_match_all() get new lines PHP
How do I assign each charactor to a variable after mb_strlen
Yii2:redirect to specific action from submit button
With Objective C and PHP, I get “Notice: Undefined index” [duplicate]
Tags with WooCommerce and Custom Code
how to open jquery ui modal tab window after successful event
Extract a value using regular expression
SQL syntax error with parsed text from html page
How does Pool::collect works?
php - How to ordering query result with answeroption float horizontal by a line
Helper function in Laravel 5
insert rss feed image to the database
How can I secure this from injection and spam
Multiple appending in an array - PHP

Categories

HOME
hpoo
elasticsearch
clearcase
focus
yii
operating-system
website
consul
cryptography
lvm
jint
getopenfilename
fbloginview
vimeo
multi-step
powermock
closures
rpmbuild
google-sites
susy-sass
varnish-vcl
renderscript
animated-gif
opentext
router
bonita
guice
infopath2010
rust-cargo
lxml
om-next
ms-media-foundation
payeezy
bean-validation
busboy
keyboard-layout
assertions
cartopy
text-decorations
iscroll
assert
python-sounddevice
spring-restcontroller
plane
uipickerview
spooler
type-inference
axis
.net-micro-framework
icecast
head.js
hibernate-ogm
amazon-elastic-beanstalk
sonatype
gtk#
google-news
email-forwarding
http4s
qtextedit
yui3
bigdecimal
decoder
carmen
cdo.message
fiber
apple-configurator
gulp-uglify
gwt-syncproxy
gravatar
vimperator
json-spirit
miniconda
blank-line
blockquote
ipod
dbmigrate
shell-extensions
mutators
sunos
iplimage
xhprof
webmethod
android-2.3-gingerbread
rails-api
jms-serializer
lync-2010
infinite
axacropdf
days
2d-vector
smartfox
story
audiotoolbox
django-apps
drawimage
delphi-prism
getopts
botnet
jmenu
gaelyk
uninstaller
sscli
cleartype
mysqli-multi-query
levels
downloadfile

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App