Possible Duplicate:
a simple function for return number from string in php
What’s the best/most efficient method to extract a specific set of numbers from a string? For example: I want to get the set of numbers immediately after Case# in sring “blah blah Case#004522 blah blah”. I imagine the number of numeric characters after Case# will always be the same but I would like the code to not make that assumption as I have been.
So far I have been using a strpos method to locate Case# and then pull a specific number of characters afterwords using substr. I just feel like this is clunky. Maybe preg_match would be more effective or simplified?
$text = "blah blah Case#004552 blah blah";
$find = strpos($text,'Case#');
if ( $find )
$numbers = substr($text, $find+5, 6);
You can make use of regular expressions to first match your pattern of characters (
Case#) and then you expect to match numbers only (digits), that is\din PCRE (Demo):For multiple (integer) matches at once: