Jump to content

Replace a substring in a string and return the whole string....how do you do it?


gregaryb

Recommended Posts

The following code is not working as I intuitivel expect it to...

$nI = strpos($strText, "'");
$strText = substr_replace($strText, "XXXX", $nI);
echo $strText

$strText contains "Greg's Native Landscapes"
After the call to substr_replace $strText contains GregXXXX
I am expecting it to contain "GregXXXXs Native Landscapes"
So how the devil is this function working?
And how do I obtain the result I am expecting?

This function is unbelievably confusing!
If I do this instead:

$nI = strpos($strText, "'");
$strText = substr_replace($strText, "XXXX", 100);
echo $strText

I get "Greg's Native LandscapeXXXX".
WTF???
Am I really going to have to code my own string library with functions that behave in the ways I am accustomed to!
 

Edited by gregaryb
Link to comment
Share on other sites

To achieve your initial goal, try this:

substr_replace("Greg's Native Landscapes","XXXX",4, 1);

4 represents the position as defined in your $n1 variable, and 1 represents the 'length' of characters to replace, which in this instance only 1, the apostrophe. 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...