Reputation: 3
I am beginner in php, and i have problems with replace & explode
I want to replace strings using ":" all strings is unknown!
input :
string1:string2:string3:string4:
have:a:good:day:
link1:link2:link3:link4:
output :
string1:string2:newString:string4:
have:a:newString1:day:
link1:link2:newString3:link4:
Upvotes: 0
Views: 283
Reputation: 2810
I think you're looking for the explode and implode functions. You can do something like this which breaks your string into an array, you can modify the array elements, then combine them back into a string.
$str = "have:a:good:day:";
$tokens = explode(":", $str);
//$tokens => ["have", "a", "good", "day", ""]
$tokens[2] = "newString1";
//$tokens => ["have", "a", "newString1", "day", ""]
$str2 = implode(":", $tokens);
//$str2 => "have:a:good:day:"
Alternatively if you just want to replace certain words in the string, you can use the str_replace function to replace one word with another. Eg.
$str = "have:a:good:day:";
$str2 = str_replace("good", "newString1", $str);
//$str2 => "have:a:newString1:day:";
Upvotes: 1