Page is a not externally linkable
- Code, Content, and Presentation
-- PHP Server Side Scripting
---- Preg match puzzler


bedlam - 5:42 pm on Nov 20, 2010 (gmt 0)


Those are very good refinements. But about this:

As "GB" is fairly commonly used, I suspect it is possible to pick it up out of the context of hard drives, so I would say the words "Hard Drive" is required.

My expression does include "Hard Drive", but Elric99 wanted to match "the hard drive size," so I'd say your sub-expression is still too inclusive.

Orignally, I assumed the unit was known, but as you correctly pointed out, it could be GB or TB these days, so I think the unit may be required in the match. If we're dealing with a source whose form may change, I'd also say the optional space character between the numbers and the unit is required.

All that said, I'd only make the following very small modifications to your regex:

/([0-9]{1,3}\s*[GT]B) Hard Drive/im

Given the above expression and the string "lorem ipsum 300 GB Hard Drive dolor sit amet", $result[0] will still return, "300 GB Hard Drive", but $result[1] will return "300 GB".

-- b


Thread source:: http://www.webmasterworld.com/php/4232346.htm
Brought to you by WebmasterWorld: http://www.webmasterworld.com