Meeting Rooms - Debt Consolidation - Credit Cards - Credit Counseling - Loans

PDA

View Full Version : How to replace a substring with another?


subeeshkk
Jan 28th 2008, 5:26 am
Hi all ,

I need to replace a sunstring with another from a string , how to do..?


I have the both the initial position and the ending position of the substring , with these input i have to do that..?

Or i have, the starting of the String and the ending position..

To make it more clear..

$str = 'First Computer is ABC, you know it';
$substr = 'is';

i want to replace 'is ABC ' with 'was XYZ' ?
Also i know , after ABC , a comma comes, so that i can take that position also,

In sshort , the start and end pos of a sub string i know , i have to replace..

Please help me

jayshah
Jan 28th 2008, 5:39 am
You can use str_replace:


$str = 'First Computer is ABC, you know it';

$find = "is ABC";
$replace = "was XYZ";

echo str_replace($find, $replace, $str);


Hope this helps,

Jay

nico_swd
Jan 28th 2008, 5:43 am
If the above doesn't work for you, have a look at substr_replace() (http://www.php.net/substr_replace).

fairuz.ismail
Jan 28th 2008, 8:26 am
normally the first solution (using the str_replace) will work with charm..it will search the substring itself without knowing the position...good luck.