Why is the returned data blank after learning staticfunction? I recently learned static & nbsp; function to build such a code. CURL obtains the webpage source code and obtains the title using regular expressions. Why does my code return nothing? Where is the problem? Can I explain it in detail? Thank you. & Nbsp; & why does the returned data of the new static function of nb Learning become null?
Recently I am studying static functions to build such a code. CURL obtains the webpage source code and obtains the title using regular expressions. Why does my code return nothing? Where is the problem? Can I explain it in detail? Thank you.
$url = 'www.php.net/archive/2012.php';
if (!$url){
exit;
}
echo TestClass::getTitle($url);
class TestClass
{
static function getTitle($url)
{
$text = self::getHtml($url);
preg_match("/()(.*)(<\/h1>)/is",$text,$h1tags);
$title = $h1tags[0];
if (!$title) return false;
return $title;
}
static function getHtml($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
$htmls = curl_exec($ch);
curl_close($ch);
if (!$htmls) return false;
return $htmls;
}
}
------ Best solution --------------------
That's your regular rule string.
/() (. *) (<\/H1>)/is
------ Other solutions --------------------
First create an object and instantiate it.
------ Other solutions --------------------
At the end of this post, xuzuning edited and run your code at 15:32:54 on February 10,. can you display the entire page?
Of course, you still need to make your code run as expected
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, true );
Before $ htmls = curl_exec ($ ch );
------ Other solutions --------------------
Reference:
This post was last edited by xuzuning at 15:32:54, January 10 ,.
Can the entire page be displayed when you run your code?
Of course, you still need to make your code run as expected
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, true );
Before $ htmls = curl_exec ($ ch );
Set, or output blank, nothing, my PHP Version 5.3.8.
I want to return the text part of the regular expression.
------ Other solutions --------------------
Reference:
First create an object and instantiate it.
What's new? No.
------ Other solutions --------------------
Maybe you have not loaded the curl extension?
------ Other solutions --------------------
Reference:
Maybe you have not loaded the curl extension?
The pure curl code can return the content of www.php.net. However, building a static function is useless.
$url = 'www.php.net/archive/2012.php';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$htmls = curl_exec($ch);
curl_close($ch);
echo $htmls;
------ Other solutions --------------------
Reference:
That's your regular rule string.
/() (. *) (<\/H1>)/is
Does it make sense to remove is?