I have many fields that are like this pattern:
re2man: (johnny bravo)
re1man: (john smith)......
user: (firstname lastname)..
I wanted to use regex, or another php function to get only characters before the colon (“:”)
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.
Look at
explode()to simply split the string at the ‘:’.For instance,
list($username) = explode(':', $string);(
list()is being used to assign just the first part of the exploded string to$usernameand discard the rest; a longer way to do it would be:$parts = explode(':', $string); $username = $parts[0];Also, you could use the extra limit parameter to indicate that you don’t need the rest of the parts, like:
list($username) = explode(':', $string, 1);That may or may not be faster. Also, I would expect using
explode()to be more efficient than some of the other regex-based solutions offered.