I’m trying to replace all spaces with underscores and the following is not working:
$id = "aa aa";
echo $id;
preg_replace('/\s+/', '_', $id);
echo $id;
prints
aa aaaa aa
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The function
preg_replacedoesn’t modify the string in-place. It returns a new string with the result of the replacement. You should assign the result of the call back to the$idvariable: