I\'m working on a function that gets a string like this:
identifier 20 j. - cat: text text text aaaa ffffdd ..... cccc 60\' - text, 2008
and
You can use a regular expression for this:
$matches = array();
preg_match('/identifier\s*(\d+)/', $string, $matches);
var_dump($matches);
\s*
is whitespace. (\d+)
matches a number.
You can wrap it in a function:
function matchIdentifier($string) {
$matches = array();
if (!preg_match('/identifier\s*(\d+)/', $string, $matches)) {
return null;
}
return $matches[1];
}
You can get the match itslef without capturing subgroups using \K
operator and ^
anchor to match the word only at the beginning of the string:
$re = "/^identifier \\K\\d+/";
$str = "identifier 20 j. - cat: text text text aaaa ffffdd ..... cccc 60' - text, 2008";
preg_match($re, $str, $matches);
echo $matches[0];
Demo is here.
Sample program is available here (PHP v5.5.18).
$string = "identifier 20 j. - cat: text text text aaaa ffffdd ..... cccc 60' - text, 2008";
$tokens = explode(' ', $string);
$token2 = $tokens[1];
if(is_numeric($token2))
{
$value = (int) $token2;
}
else
{
$value = NULL;
}