Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.