Reputation: 187
I need to get the value from an input field, find the first number on it, and remove everything else BEFORE the number.
But I need to leave the number (and anything after it) as it is.
I tried some options, but couldn't do it.
Any ideas?
I managed to find where is the first number. But when I try to remove everything BEFORE it, I endup removing the first number as well, or removing another stuff (like periods and commas)
Here is how I got the first number position:
$stringPrice = '__.500,00';
if(preg_match('/[0-9]/', $stringPrice, $positionNum, PREG_OFFSET_CAPTURE)) {
echo "First number is at " . $positionNum[0][1];
} else {
echo "Invalid value";
}
Upvotes: 1
Views: 1181
Reputation: 626738
Your current issue is to remove all non-numeric chars from the start of the string up to the first digit.
Use
preg_replace('/^\D+/', '', $stringPrice);
See the PHP demo
The ^
makes sure matching starts at the start of the string and \D+
matches one or more chars other than digits.
Upvotes: 3