开发者

preg_match , regexp , php , extract text from html

I'm trying to extract "Florida (FL)" from http://www.auctionarms.com/search/displayitem.cfm?itemnum=9736364&oh=216543. My code is

//get loca开发者_运维技巧tion
   $pattern = "/(State)</i>:</td>(.*)</td>/";
   preg_match_all($pattern, $htmlContent, $matches);
   print_r($matches);
any idea why is not working ?


When you have (State) in a regex, it will match the term State in the input string as a group, it won't match literal parenthesis in the input - you'll need to escape them as you have with the /s - /\(State\)<\/....

Then there's the problem that there's lots of whitespace around (including new lines - you'll need to include the m modifier), and a <b/> tag around the header which you seem to have not included in the regex. Even if you fix these problems, you're highly reliant on the exact markup used by the website you're scraping. This is a general problem you'll encounter when trying to parse HTML using regular expressions. It would be a better idea to use a HTML parser (e.g. creating a new DOMDocument and calling its loadhtml method).


I believe the reason is because the string you're trying to match is on the next line. You'll need to enable multi-line mode with:

$pattern = "/\(State\)<\/i>\:<\/td>(.*)<\/td>/m";

But remember: attempting to parse HTML with regular expressions makes the unholy child weep the blood of virgins. See:

RegEx match open tags except XHTML self-contained tags

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜