简体   繁体   中英

get word before a :- sign in a string with regex

I need to get the amount before a :- sign. So the string would be: bla bla 120:-

And then store only 120 in a variable

preg_match_all('!(\d+):-!', $string, $matches);
print_r($matches);

This should do it. It captures anything up to a whitespace before ":-"

The regex

/(-?\d+):-/

will capture any digits (and a negative sign, if it's there) before a ":-" in the string.

You can than parse that into a number and store it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM