Reputation: 402
I need to match everything but not the white spaces
For example in this string : 16790 - 140416 / 3300
I want to have the following result made by regex (and PHP) without white spaces: 140416/3300
I used : \s+\d+\s+-\s+(\d+\s+\/\s+\d+)
and of course it gave me the results with the whitespaces:
140416 / 3300
How could I have a match 1 result without white spaces ?
Thank you
Upvotes: 0
Views: 52
Reputation: 41838
If you're just removing an unknown number of spaces and tabs from a string, you can use
$result = preg_replace('~\s*~', '', $subject);
If you're matching the whole pattern you gave (not just the division), you can use this:
$result = preg_replace('~(\d+)\s*-\s*(\d+)\s*/\s*(\d+)~', '\1-\2/\3', $subject);
Finally, and this might be the best, if you'd like to remove spaces around operators such as =,*,-,/ when they are surrounded by digits, you can use this:
$result = preg_replace('~(\d)\s*([+*/-])\s*(\d)~', '\1\2\3', $subject);
Upvotes: 1
Reputation: 98961
$subject = "16790 - 140416 / 3300";
$result = preg_replace('%.*?(\d+)\s+/\s+(\d+)%', '$1/$2', $subject);
echo $result;
// 140416/3300
Upvotes: 1