首页 > 编程技术 > php

php检测函数是否存在函数 function_exists

发布时间:2016-11-25 16:50

如果你要判断一些函数是不是可以用我们可以使用function_exists函数来检测哦,下面我们来看几个例子,希望对你会有帮助。

语法
bool function_exists ( string $function_name )
检查的定义的函数的列表,同时内置(内部)和用户定义的,为function_name。
返回值

返回true,如果function_name存在,是一个函数,否则返回false。
*/

if (function_exists('imap_open')) {
    echo "imap functions are available.www.111cn.net<br /> ";
} else {
    echo "imap functions are not available.<br /> ";
}


//function_exists returns false on null and empty string:

if (function_exists('')) {
                echo "empty string function exists ";
        }

        if (function_exists(null)) {
                echo "null function exists ";
        }
  
//如果您使用suhosin.executor.func.blacklist而不是在你的php.ini disabled_functions,function_exists将返回true为功能。我用这个有与suhosin.executor.func.blacklist和disabled_functions相同beahviour:

function suhosin_function_exists($func) {
    if (extension_loaded('suhosin')) {
        $suhosin = @ini_get("suhosin.executor.func.blacklist");
        if (empty($suhosin) == false) {
            $suhosin = explode(',', $suhosin);
            $suhosin = array_map('trim', $suhosin);
            $suhosin = array_map('strtolower', $suhosin);
            return (function_exists($func) == true && array_search($func, $suhosin) === false);
        }
    }
    return function_exists($func);
}

如果我们要判断一个类是不是可以用,可以先使用class_exists函数来判断一下,下面来看几个例子。

//bool class_exists ( string $class_name [, bool $autoload = true ] )
//此功能是否给定的类被定义检查。this function checks whether or not the given class has been defined.

//返回true,如果class_name是一个定义的类,否则返回false。
//实例

 代码如下 复制代码
if (class_exists('myclass')) {
    $myclass = new myclass();
}

 

 代码如下 复制代码

function __autoload($class)
{
    include($class . '.php');

    // check to see whether the include declared the class
    if (!class_exists($class, false)) {
        trigger_error("unable to load class: $class", e_user_warning);
    }
}

if (class_exists('myclass')) {
    $myclass = new myclass();
}

 代码如下 复制代码
$post =1;
$url = parse_url($url);
$host ='http://www.111cn.net';
$path ='/';
$query ='?action=111cn.net';
$port =80;
 
if($post) {
  $out = "post $path http/1.0 ";
  $out .= "accept: */* ";
  //$out .= "referer: $boardurl ";
  $out .= "accept-language: zh-cn ";
  $out .= "content-type: application/x-www-form-urlencoded ";
  $out .= "user-agent: $_server[http_user_agent] ";
  $out .= "host: $host ";
  $out .= 'content-length: '.strlen($post)." ";
  $out .= "connection: close ";
  $out .= "cache-control: no-cache ";
  $out .= "cookie: $cookie ";
  $out .= $post;
 } else {
  $out = "get $path http/1.0 ";
  $out .= "accept: */* ";
  //$out .= "referer: $boardurl ";
  $out .= "accept-language: zh-cn ";
  $out .= "user-agent: $_server[http_user_agent] ";
  $out .= "host: $host ";
  $out .= "connection: close ";
  $out .= "cookie: $cookie ";
 }
 $fp = @fsockopen(($ip ? $ip : $host), $port, $errno, $errstr, $timeout);
 if(!$fp)
 {
  return '';//note $errstr : $errno
 } else {
  return '成功访问';
 }
 /*
 fsockopen语法
 
 resource fsockopen ( string $hostname [, int $port = -1 [, int &$errno [, string &$errstr [, float $timeout = ini_get("default_socket_timeout") ]]]] )


 
 启动一个套接字连接到指定的主机的资源。
    php支持在互联网领域的目标和unix在所支持的套接字传输列表说明。所支持的传输列表也可以检索使用stream_get_transports()。
 
 该插座预设会被启用,阻塞模式。你可以切换到非阻塞模式使用stream_set_blocking()。
 
 如果上面实例看不懂,就来看个简的吧


*/

 

 代码如下 复制代码
$fp = fsockopen("www.111cn.net", 80, $errno, $errstr, 30);
 if (!$fp) {
  echo "$errstr ($errno)<br /> ";
 } else {
  $out = "get / http/1.1 ";
  $out .= "host: www.111cn.net ";
  $out .= "connection: close ";
  fwrite($fp, $out);
  while (!feof($fp)) {
   echo fgets($fp, 128);
  }
  fclose($fp);
 }

//需求:在所有连接后面添加一个request=xxx; 这个函数比preg_replace灵活性更强,要注意它所替换的内容为整个正则表达式的内容。
$content = '<a href="http://www.111cn.net/aaa.php">链接1</a><a href="http://www.111cn.net/aaa.php?id=111">链接2</a>';
function add_source($matches)
{
    if(strpos($matches[1], '?'))
    {
        return 'href="'.$matches[1].'&request=xxx"';  //注意,这里和下面都加上了正则里括号外的东西:href="
    }
    else
    {
        return 'href="'.$matches[1].'?request=xxx"';
    }
}
$content = preg_replace_callback('/href=['|"](.*?)['|"]/', 'add_source', $content);


//实例二


  // 此文本是用于 2002 年的,
  // 现在想使其能用于 2003 年
  $text = "april fools day is 04/01/2002 ";
  $text.= "last christmas was 12/24/2001 ";

  // 回调函数
  function next_year($matches) {
    // 通常:$matches[0] 是完整的匹配项
    // $matches[1] 是第一个括号中的子模式的匹配项
    // 以此类推
    return $matches[1].($matches[2]+1);
  }

  echo preg_replace_callback(
              "|(d{2}/d{2}/)(d{4})|",
              "next_year",
              $text);

  // 结果为:
  // april fools day is 04/01/2003
  // last christmas was 12/24/2002

//preg_replace ( mixed $pattern , mixed $replacement , mixed $subject [, int $limit = -1 [, int &$count ]] ) 主题为匹配搜索模式,替换替换
/*
要搜索的模式。它可以是一个字符串或一个字符串数组。

电子修饰符使preg_replace函数()替代治疗后,适当引用作为参数是php教程代码进行替换。提示:请确保置换构成一个有效的php代码字符串,否则php将抱怨在包含preg_replace函数线()解析错误。

返回值

preg_replace函数()返回一个数组,如果这个问题的参数是一个数组或一个字符串,否则。

如果找到匹配,新问题会产生,否则将返回主题不变或null如果发生错误。
*/

//实例一

 代码如下 复制代码
$string = 'april 15, 2003';
$pattern = '/(w+) (d+), (d+)/i';
$replacement = '${1}1,$3';
echo preg_replace($pattern, $replacement, $string);

//实例二

 代码如下 复制代码
$string = 'the quick brown fox jumped over the lazy dog.';
$patterns = array();
$patterns[0] = '/quick/';
$patterns[1] = '/brown/';
$patterns[2] = '/fox/';
$replacements = array();
$replacements[2] = 'bear';
$replacements[1] = 'black';
$replacements[0] = 'slow';
echo preg_replace($patterns, $replacements, $string);

//通过ksorting模式和替代,我们应该得到我们想要的。

 代码如下 复制代码
ksort($patterns);
ksort($replacements);
echo preg_replace($patterns, $replacements, $string);

//更换几个值

 代码如下 复制代码
$patterns = array ('/(19|20)(d{2})-(d{1,2})-(d{1,2})/',
                   '/^s*{(w+)}s*=/');
$replace = array ('//', '$ =');
echo preg_replace($patterns, $replace, '{startdate} = 1999-5-27');

//过滤所有html 标签

 代码如下 复制代码
preg_replace("/(</?)(w+)([^>]*>)/e",
             "'\1'.strtoupper('\2').'\3'",
             $html_body);

//过滤所有script代码

 代码如下 复制代码

$user_agent = "mozilla/4.0 (compatible; msie 5.01; windows nt 5.0)";

$ch = curl_init();    // initialize curl handle
curl_setopt($ch, curlopt_url, $url); // set url to post to
curl_setopt($ch, curlopt_failonerror, 1);              // fail on errors
curl_setopt($ch, curlopt_followlocation, 1);    // allow redirects
curl_setopt($ch, curlopt_returntransfer,1); // return into a variable
curl_setopt($ch, curlopt_port, 80);            //set the port number
curl_setopt($ch, curlopt_timeout, 15); // times out after 15s

curl_setopt($ch, curlopt_useragent, $user_agent);

$document = curl_exec($ch);

$search = array('@<script[^>]*?>.*?</script>@si',  // strip out javascript教程 www.111cn.net
'@<style[^>]*?>.*?</style>@siu',    // strip style tags properly
'@<[/!]*?[^<>]*?>@si',            // strip out html tags
'@<![ss]*?–[ ]*>@',         // strip multi-line comments including cdata
'/s{2,}/',

);

$text = preg_replace($search, " ", html_entity_decode($document));

$pat[0] = "/^s+/";
$pat[2] = "/s+$/";
$rep[0] = "";
$rep[2] = " ";

$text = preg_replace($pat, $rep, trim($text));

return $text;
}

/*
此函数接受一个url并返回页面的纯文本版本。它使用curl来检索网页,一个正则表达式的组合,以去除所有不必要的空白。这个功能甚至剥夺了从形式和script标记,这是由php函数忽略,如用strip_tags(他们地带唯一的标记文本,留下完整的文字在中间)。

正则表达式被分为两个阶段,以避免删除单(也由 s的匹配)回车,但仍然删除所有空白行和多个换行符或空格,修整手术进行了2个阶段进行。
*/
?>

标签:[!--infotagslink--]

您可能感兴趣的文章: