I have a string :
<div id="post_message_957119941">
I want to fetch only the numbers (957119941) from this string using preg_match
.
Return Values ¶ preg_match() returns 1 if the pattern matches given subject , 0 if it does not, or false on failure. This function may return Boolean false , but may also return a non-Boolean value which evaluates to false . Please read the section on Booleans for more information.
We can use the preg_replace() function for the extraction of numbers from the string.
preg_match stops looking after the first match. preg_match_all , on the other hand, continues to look until it finishes processing the entire string. Once match is found, it uses the remainder of the string to try and apply another match.
Definition and Usage The preg_match() function returns whether a match was found in a string.
This shouldn't be too hard.
$str = '<div id="post_message_957119941">';
if ( preg_match ( '/post_message_([0-9]+)/', $str, $matches ) )
{
print_r($matches);
}
Output:
Array ( [0] => post_message_957119941 [1] => 957119941 )
So the desired result will always be in: $matches[1]
Is that what you need?
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