I have lines of text like so:
CPU: Intel Core i7 970 $580
Mobo: Gigabyte X58A-UD5 $300
RAM: Kingston 6x 4GB DDR3 $230
HDD: Samsung Spinpoint F3 1TB $70
GPU: 1GB Nvidia GeForce GTS450 $113
Case: Antec P183 $135
PSU: Seasonic M12II 520W $113
ODD: Samsung SG-B083A $29
CPU HSF: Coolermaster Hyper 212 $30
I wish to remove the CPU:, Mobo: etc bits (so basically anything behind : [notice the space]) .
Further to this, I wish to remove the price bit on the end – $580 etc.
Something like this should work right?
preg_replace('/\$([0-9\.]+)/', $PC_Specs);
It’s so I can store the product name in a variable.
Thanks greatly.
Try something more like:
For one thing I added the empty string as a second parameter, which is needed to tell preg_replace what to replace the matches with. I also modified the regex to match two distinct sub expressions:
^\w+:\s*Matches start of line with at least one word character followed by a colon and 0 or more spaces.\$(\d*\.?\d+\s*$Matches a dollar sign followed by a number with a possible decimal point and zero or more spaces before the end of the line.I added the
/mmodifier to make it multiline regex too, which you need if you are passing the whole thing in as one. If you are passing in an array of lines then you can remove that.EDIT In response to comment:
After the above code runs,
$cpuwill be"Intel Core i7 970"$mobowill be"Gigabyte X58A-UD5"$ramwill be"Kingston 6x 4GB DDR3"etc… 🙂