I am trying to learn regex. I have the string:
$x = "5ft2inches";
How can I read [5,2] into an array using a regex?
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.
If you are assuming that the string will be of the form “{number}ft{number}inches” then you can use preg_match():
(\d+)will match a string of one or more digits. The parentheses will tellpreg_match()to place the matched numbers into the$matchesvariable (the third argument to the function). The function will return 1 if it made a match, of 0 if it didn’t.Here is what
$matcheslooks like after a successful match:The entire matched string is the first element, then the parenthesized matches follow. So to make your desired array: