gpt-3.5-turbo-0125· OpenAI
WildChat-4.8M (AI2)
In PHP, using regular expressions, how to retrieve digits in a regular expressions ?
- User
In PHP, using regular expressions, how to retrieve digits in a regular expressions ?
- Assistant
To retrieve digits using regular expressions in PHP, you can use the following code:
$string = "abc123def456ghi"; // Use preg_match_all to find all digits in the string preg_match_all('/\d+/', $string, $matches); // $matches[0] will contain an array of all the digits found in the string foreach($matches[0] as $match) { echo $match . "<br>"; }In this code, the regular expression
/d+is used to match one or more digits in the string. Thepreg_match_allfunction is used to find all occurrences of this pattern in the string, and store them in the$matchesarray. Finally, the digits are printed out one by one using a foreach loop.