I'm trying to replace the first character of a string with words, but I'm running into some trouble here. I'm only able to replace the character with the first character of the string, and not the entire string. How would I fix this?
$type = "xgo xgo xgo";
$ifX = $type[0];
if ($ifX == "x") {
$type[0] = "do not ";
}
dgo xgo xgo
do not go xgo xgo
Try this,
$type = "xgo xgo xgo";
echo preg_replace('/x/', 'do not ', $type, 1); // output : do not go xgo xgo
If you dont specity 4th parameter, your output looks like this
do not go do not go do not go// all x are replaced.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.