php - Using regex to extract numbers and symbols from string -
i have string text, numbers, , symbols. i'm trying extract numbers, , symbols string limited success. instead of getting entire number , symbols, i'm getting part of it. explain regex below, make more clearer, , easier understand.
\d : number [+,-,*,/,0-9]+ : 1 or more of +,-,*,/, or number \d : number
code:
$string = "text 1+1-1*1/1= text"; $regex = "~\d[+,-,*,/,0-9]+\d~siu"; preg_match_all($regex, $string, $matches); echo $matches[0][0];
expected results
1+1-1*1/1
actual results
1+1
remove u flag. it's causing the +
nongreedy in matching. also, don't need commas between characters in character list. (you need 1 ,
if you're trying match it. need escape -
doesn't think you're trying make range
Comments
Post a Comment