Why is the returned data of the newly learned staticfunction empty?

Source: Internet
Author: User
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?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.