How do I remove everything after a space in PHP?
问题 I have a database that has names and I want to use PHP replace after the space on names, data example: $x="Laura Smith"; $y="John. Smith" $z="John Doe"; I want it to return Laura John. John 回答1: Do this, this replaces anything after the space character. Can be used for dashes too: $str=substr($str, 0, strrpos($str, ' ')); 回答2: Just to add it into the mix, I recently learnt this technique: list($s) = explode(' ',$s); I just did a quick benchmark though, because I've not come across the strtok