-1

regular expression

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

Someone can fixe this mess ?

$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>
Community
  • 1
  • 1
  • Need some cheers from the community? Don't underestimate yourself, I'm sure you'll do just fine ! – Robin Apr 17 '14 at 20:01
  • i spent 4 hours but didnt work !!! – user3546707 Apr 17 '14 at 20:02
  • what language and what input data does it have? – P0ZiTR0N Apr 17 '14 at 20:02
  • @user3546707: Do you have a question? – Robin Apr 17 '14 at 20:03
  • im try to get current city from facebook but doesnt work – user3546707 Apr 17 '14 at 20:04
  • **Don't use regular expressions to parse HTML. Use a proper HTML parsing module.** You cannot reliably parse HTML with regular expressions, and you will face sorrow and frustration down the road. As soon as the HTML changes from your expectations, your code will be broken. See http://htmlparsing.com/php or [this SO thread](http://stackoverflow.com/questions/3577641/how-do-you-parse-and-process-html-xml-in-php) for examples of how to properly parse HTML with PHP modules that have already been written, tested and debugged. – Andy Lester Apr 17 '14 at 20:05
  • i would use specific div id class , it wont work – user3546707 Apr 17 '14 at 20:12

2 Answers2

0

Try this:

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

Here's a demo of the regex that I've used.

Chirag Bhatia - chirag64
  • 4,430
  • 3
  • 26
  • 35
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;
Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268