Reputation: 3021
I have a $value such as 22214-HAV.jpg or 22214 HAV.jpg (notice no dash)
I want to run a quick function to pull only the number from filename.
Upvotes: 1
Views: 208
Reputation: 132071
A quick solution, which make use of PHPs type juggling
$number = (int) $filename;
Upvotes: 5
Reputation: 44386
preg_match('/^\d+/' ,'22214-HAV.jpg', $matches);
var_dump($matches[0]);
Observations:
^
. ([1-9]\d*|0)
in place of \d+
. $matches[0]
will be null
and not an empty string.Further reading:
Upvotes: 4
Reputation: 3678
You can use explode for this
//for '-'
list($reqval)=explode('-', $value);
//for space
list($reqval)=explode(' ', $value);
echo $reqval
Upvotes: 1