正则表达式/ \ <div id =“\”current_city \“”class =“\”current_city \“\”>(。*?)\&lt; \ / div \&gt; / </div>

时间:2014-04-17 19:59:54

标签: regex html-parsing

正则表达式

  

/\<div id=\"current_city\" class=\"current_city\"\>(.*?)\<\/div\>/

有人可以解决这个烂摊子吗?

  

$ cookie_file_path =“fb.txt”;

     

$ fbplink1 = “https://www.facebook.com/profile/about”;

     

$ fbplinkfinal = preg_replace('/ \ s + /','',$ fbplink1);

$ch = curl_init();

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);

curl_setopt($ch, CURLOPT_HEADER, false);

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

curl_setopt($ch, CURLOPT_URL, $fbplinkfinal);

curl_setopt($ch, CURLOPT_REFERER, $fbplinkfinal);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);

curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);   
     

curl_setopt($ ch,CURLOPT_USERAGENT,'Mozilla / 5.0(Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13)Gecko / 20080311 Firefox / 2.0.0.13');

$result = curl_exec($ch);

curl_close($ch); $Getcurrentcity=preg_match_all("\<div id='current_city' class\=(.*?)\>(.*?)\<\/div\>",$result,$currentcity);

<textarea value="<?echo $currentcity[0][0];?>" ></textarea>

2 个答案:

答案 0 :(得分:0)

试试这个:

<?php
$elem = $dom->getElementById("current_city");
$textData = $elem->textContent;
preg_match("/(?<=Edit).+?(?=,)/", $textData, $city);
echo $city[0];
?>

这是我使用的正则表达式demo

答案 1 :(得分:0)

$cookie_file_path = "fb.txt";
$fbplink1="https://www.facebook.com/profile/about";
$fbplinkfinal = preg_replace('/\s+/', '', $fbplink1);
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_URL, $fbplinkfinal);
curl_setopt($ch, CURLOPT_REFERER, $fbplinkfinal);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);   
curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US;rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
$result = curl_exec($ch);
curl_close($ch);


preg_match_all('%<div id="current_city" class="current_city">(.*?)</div>%', $buffer, $result, PREG_PATTERN_ORDER);
$result = $result[1][0];

echo $result;