Jump to content

Editing certain parts of a string


AbydosGater

Recommended Posts

Hi,

I am working on a script, and for it i need to edit certain occurances of a string within another string. Its not your basic str_replace type of thing. I will have a string like:

 

"Hi, How are you today, are you ok?, i hope you are"

 

Ehh, well not exactly like that haha but thats only an example. And with that i want to replace just say the second "you" I think i have it sorted out about how to get the position of only the second "you" with my own strpos function.

But how when i have the pos, how would i edit only that part of the string?

 

Andy

Link to comment
https://forums.phpfreaks.com/topic/52812-editing-certain-parts-of-a-string/
Share on other sites

OK in simple terms...What!

 

you want to replace the 2nd you with something.. ok lets say i wanted to replace the 2nd you with me,

 

<?php

$yourStr = "Hi, How are you today, are you ok?, i hope you are";
$find = "you";
$replace = "Me";
$Occ = 2;

$LPos = 0;
for($n=0;$n<$Occ;$n++)
{
$LPos = strpos(strtolower($yourStr), strtolower($find), $LPos+1);
}
$MyStr = substr($yourStr, 0, $LPos);
$MyStr .= $replace;
$MyStr .= substr($yourStr, $LPos+strlen($find), strlen($yourStr));


echo $yourStr."<br />";
echo $MyStr;

die;
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.