我必须从此网站获取号码的位置
lookupexpert.com/search_phone?phone_number=7322691678
我想要一个匹配
内的任何内容的正则表达式<p class="location">OCEAN GATE, NJ</p>
我该怎么做?
这是我到目前为止所做的事情
<?php
$subject = file_get_contents("http://lookupexpert.com/search_phone?phone_number=7322691678");
$pattern = '#\<p class="location"\>(.+?)\<\/p\>#s';
preg_match($pattern, $subject, $matches, PREG_OFFSET_CAPTURE, 3);
print_r($matches);
?>
并且......我尝试使用Xpath,但没有那么好用,因为它没有经过适当的验证
/html/body/div/div[2]/div/ul/li[2]/p[4]
答案 0 :(得分:3)
试试这个:
$subject = file_get_contents( 'http://www.lookupexpert.com/search_phone?phone_number=7322691678');
preg_match_all( '#<p class="location">(.*?)</p>#', $subject, $matches);
var_dump( $matches[1][1]);
输出:
string(14) "OCEAN GATE, NJ"
答案 1 :(得分:2)
使用此XPath
//p[@class='location']/text()
或此RegEx
(?<=<p class="location">)([^<>]+)(?=</p>)
<强>码强>
preg_match_all('%(?<=<p class="location">)([^<>]+)(?=</p>)%', $subject, $result, PREG_PATTERN_ORDER);
$result = $result[1];
答案 2 :(得分:1)
试试这个..
$string = '<p class="location">OCEAN GATE, NJ</p>';
$pattern = '/<p class="location">(.*)<\/p>/';
$preg = preg_match_all($pattern, $string, $match);
print_r($match);
答案 3 :(得分:1)
最好不要依赖不可靠的正则表达式来解析HTML并使用DOM解析器。使用这样的代码:
$doc = new DOMDocument();
libxml_use_internal_errors(true);
// assuming search_phone.html contains your saved HTML source
#$doc->loadHTMLFile('search_phone.html'); // loads your html
$xpath = new DOMXPath($doc);
$value = $xpath->evaluate("string(//li[starts-with(@class, 'recordItem')]/
p[@class='location']/text())");
echo "Location Name: [$value]\n"; // prints your location
Location Name: [OCEAN GATE, NJ]