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

filesmatch htaccess convert to nginx (on plesk)
Fetching orders details throught flipkart api using PHP(how to recall function without rewrite function declaration)
how base64 converts characters that aren't existing in its character list?
How to read data from XML file using php
How can I limit the parameters of the GET values to the ones in my array?
configure php from Redhat to connect to MS SQL Server
What is the meaning of “->” in PHP? [duplicate]
How can i sleep the execution of each iteration of for loop in php
How to store api url in zf2 local.php [closed]
php variable not saving output of query
RewriteRule: bad flag delimiter
Popup Button not working inside Ajax success via JSON
How to execute Jess in php
How to get different Wordpress category links within one php file?
What should an HTML 5 form submit when no value is set [duplicate]
How to present enum table property in Laravel Model

Categories

HOME
opengl
oracle
memory
typelite
redmine
operating-system
mousemove
dafny
embedded-resource
out-of-memory
elk-stack
material-components
visualforce
specifications
cocotb
hspi
amazon-emr
clish
vimeo
cname
mpu6050
global-variables
fosuserbundle
playframework-2.0
word2vec
cloudsim
cpu-usage
spam
error-logging
tableview
altium-designer
react-dnd
high-availability
acrobat
intune
netbios
rust-cargo
sca
gsp
serversocket
cloveretl
image-optimization
apptentive
transform
xvfb
macports
jacoco-maven-plugin
grails3.2.0
vb.net-to-c#
tinyioc
non-linear-regression
renderman
scala-collections
mechanicalturk
cognos-tm1
perl-data-structures
email-injection
libharu
p6spy
ftp-client
raw-sockets
android-sdcard
android-syncadapter
uibinder
jeasyui
i2b2
android-expansion-files
helm
largenumber
blank-line
canopy
gitlab-omnibus
wicked-gem
deque
htmltidy
createfile
utf8-decode
image-rotation
window-resize
loginview
angular-ui-select
forio-contour
commenting
spring-social-linkedin
strdup
pbkdf2
yii-booster
disjoint-union
ax
node.js-stream
coordinate
background-repeat
will-paginate
server-variables
filelock
canonicalization
kohana-auth
simile
mygeneration
ios-4.2
urchin
spring-modules
subtext
accumulator
ning
stretchblt
prism-2
objectinstantiation
gui-designer
parentid

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