AbydosGater Posted May 24, 2007 Share Posted May 24, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/52812-editing-certain-parts-of-a-string/ Share on other sites More sharing options...
MadTechie Posted May 24, 2007 Share Posted May 24, 2007 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; ?> Quote Link to comment https://forums.phpfreaks.com/topic/52812-editing-certain-parts-of-a-string/#findComment-260755 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.