I want to search and replace the first word with another in php like as follows:
$str="nothing inside";
Replace ‘nothing’ to ‘something’ by search and replace without using substr
output should be: ‘something inside’
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.
Use
preg_replace()with a limit of 1:Replace the regular expression
/nothing/with whatever string you want to search for. Since regular expressions are always evaluated left-to-right, this will always match the first instance.