I have this string
@[123:peterwateber] hello there 095032sdfsdf! @[589:zzzz]
I want to get 123 and 589 how do you that using regular expression or something in PHP?
Note: peterwateber and zzzz are just examples. any random string should be considered
Don't forget the lookahead so you don't match 095032
:
$foo = '@[123:peterwateber] hello there 095032sdfsdf! @[589:zzzz]';
preg_match_all("/[0-9]+(?=:)/", $foo, $matches);
var_dump($matches[0]); // array(2) { [0]=> string(3) "123" [1]=> string(3) "589" }
The following regex will extract one or more numeric characters in a row:
preg_match_all('#\d+#', $subject, $results);
print_r($results);
There is a function called preg_match_all
The first parameter accepts a regular expression - The following example shows 'match at least one digit, followed by any number of digits. This will match numbers.
The second param is the string itself, the subject from which you want extraction
The third is an array into which all the matched elements will sit. So first element will be 123, second will be 589 and so on
preg_match_all("/[0-9]+/", $string, $matches);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With