I have a regexp I’m using with sed, but now I need to make it work in PHP also. I can’t use system calls as they are disabled.
$ cat uglynumber.txt: Ticket number : 303905694, FOO:BAR:BAR: Some text Case ID:123,456,789:Foobar - Some other text 303867970;[FOOBAR] Some text goes here Case Ref: 303658850 - Some random text here - host.tld #78854w
$ cat uglynumbers.txt | sed 's/[, ]//g;s/.*\([0-9]\{9\}\).*/\1/g' 303905694 123456789 303867970 303658850
So, how to do the same with PHP?
I found one example like this, but I can’t inject that regexp into that.
if (preg_match('/.../', $line, $matches)) { echo 'Match was found'; echo $matches[0]; }
Your specific SED example is obviously 2 regular expressions, 1 being replacing the commas, and one being technically grabbing the 9 digit continuous numbers.
The first half of your SED string is best fit with the
preg_replace()function.The second half of your SED string would be a
preg_match_all():So your specific code will look something like:
It looks like
gis an SED modifier meaning match all lines.preg_match_all()should already takes care of this modifier butmseems like an appropriate replacement as per the manual on PCRE modifiers.