I have a mixed string such as:
Job number 45752 Subtotal price $937.50
Job number 7852 Subtotal amount $637.50
Job number 42 Subtotal test $427.50
Job number 47592 Subtotal sample $976.50
How do I detect the last alphabet character like the 1st sample 'e' on price, and get its position and then remove all other characters in front?
I know strpos
can be used to find the last character
strpos(string,find,start)
But how to make it to track any alphabet instead of a fixed one? I am guessing regex might help but just no idea how to put it in. Please help.
s1. trim() . trim() removes spaces before the first character (which isn't a whitespace, such as letters, numbers etc.)
Using 'str. replace() , we can replace a specific character. If we want to remove that specific character, replace that character with an empty string. The str. replace() method will replace all occurrences of the specific character mentioned.
With regex preg_replace() function, using flags i
caseless and m
multi-line mode:
To replace from ^
line start to the last alpha with optional spaces, put a greedy dot before [a-z]
$str = preg_replace('/^.*[a-z]\h*/im', "", $str);
\h*
matches any amount of horizontal space. See test at regex101, eval.in, regex quickstart
I think this can helps:
/.* ([^a-z]+)$/igm
[Regex Demo]
or
/([^a-z]+)$/igm
[Regex Demo]
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