PHP之重要函数

积少成多——作者:zccst


10,判断必须是整数和是否是内网ip

if(strcmp($param,(int)$param) === 0){
echo true;
}else{
echo false;
}
/** 内网IP地址:
* 10.0.0.0 到 10.255.255.255
* 172.16.0.0 到 172.31.255.255
* 192.168.0.0 到 192.168.255.255
*/
public static function IsInternalIp($ip){
$bResult = false ;
$aIpSections = array(
'10.0.0.0' => '10.255.255.255' ,
'172.16.0.0' => '172.31.255.255' ,
'192.168.0.0' => '192.168.255.255' ,
);
foreach($aIpSections as $strStart => $strEnd){
if( (ip2long($strStart) <= ip2long($ip)) && (ip2long($ip) <= ip2long($strEnd)) ){
return true ;
}
}

return $bResult ;/*}}}*/
}


9,根据网段得出掩码

$record = split('/', $data['name']);
$mask = 0xffffffff << (32 - $record[1]);
$data['mask'] = long2ip($mask);


8 , string base64_encode(string data);
本函数将字符串以 MIME BASE64 编码。此编码方式可以让中文字或者图片也能在网络上顺利传输。在 BASE64 编码后的字符串只包含英文字母大小写、阿拉伯数字、加号与反斜线,共 64 个基本字符,不包含其它特殊的字符,因而才取名 BASE64。编码后的字符串比原来的字符串长度再加 1/3 左右。更多的 BASE64 编码信息可以参考 RFC2045 文件之 6.8 节。

7,similar_text
similar_text() 函数计算两个字符串的匹配字符的数目,也可以计算两个字符串的相似度(以百分比计)。
参数 描述
string1 必需。规定要比较的第一个字符串。
string2 必需。规定要比较的第二个字符串。
percent 可选。规定供存储百分比相似度的变量名。

similar_text("Hello World","Hello Peter",$percent);
echo $percent;

输出:63.6363636364


6,spl_autoload_register('autoloader');

function autoloader($className){
if(file_exists(dirname(__FILE__)."/$className.php")){
include dirname(__FILE__)."/$className.php";
if(class_exists($className,false)){
return true;
}
}
return false;
}

批注:
bool class_exists ( string $class_name [, bool $autoload = true ] )
第二个参数:Whether or not to call __autoload by default.
只有当第二个参数为false时,才能不开启autoload,以节省开支。
__autoload机制:
参阅另一篇文章:php autoload机制。


[b]5,addslashes()和stripslashes()[/b]
addslashes() 函数在指定的预定义字符前添加反斜杠。 这些预定义字符是: 单引号 (') 双引号 (") 反斜杠 (\) NULL

$str = "Is your name O'reilly?";

// Outputs: Is your name O\'reilly?
echo addslashes($str);

stripslashes() 函数删除由 addslashes() 函数添加的反斜杠

[b]4,get_magic_quotes_gpc()[/b]
int get_magic_quotes_gpc ( void )
Returns the current configuration setting of magic_quotes_gpc
Keep in mind that attempting to set magic_quotes_gpc at runtime will not work.
For more information about magic_quotes, see this security section.
批注:取得 PHP 环境变量 magic_quotes_gpc(GPC, Get/Post/Cookie) 的值。返回 0 表示关闭本功能;返回 1 表示本功能打开。
当 magic_quotes_gpc 打开时,所有的 ' (单引号), " (双引号), \ (反斜线) and 空字符会自动转为含有反斜线的转义字符。


当magic_quotes_gpc = On时,系统会自动处理单引号等问题,用不用addslashes()和stripslashes()都没关系,但是如果添加数据时用了addslashes(),那么显示数据时必须要stripslashes()

当magic_quotes_gpc = Off时,系统不会处理单引号等问题,所以插入数据时必须要使用addslashes(),显示数据时则不需要使用stripslashes()。

既然有了分析,做程序时要怎么办呢?根据以上两种情况,可得:

不管magic_quotes_gpc是On还是Off,咱添加数据时都用addslashes(),当On时,必须使用stripslashes(),Off时则不能用stripslashes()。

如何判断On还是Off呢?用get_magic_quotes_gpc()。

最后举例:

//1,存入数据库
$Content=addslashes(”这里面是数据,不管有没单引号或者还是变量”);
//插入数据到数据库,代码省略

//2,从数据库取
$Content=”从数据库读取的数据”;
if(get_magic_quotes_gpc()){
$Content=stripslashes($Content);
}
echo $Content;

/************ 例子2 **************/
echo get_magic_quotes_gpc(); // 1
echo $_POST['lastname']; // O\'reilly
echo addslashes($_POST['lastname']); // O\\\'reilly

if (get_magic_quotes_gpc()) {
$lastname = stripslashes($_POST['lastname']);
}
else {
$lastname = $_POST['lastname'];
}

// If using MySQL
$lastname = mysql_real_escape_string($lastname);

echo $lastname; // O\'reilly
$sql = "INSERT INTO lastnames (lastname) VALUES ('$lastname')";



[b]3,get_file_contents($fileName)[/b]
把文件读入一个字符串
file_get_contents() 函数是用来将文件的内容读入到一个字符串中的首选方法。

file_put_contents(string $filename , string $data [, int $flags [, resource $context ]] )
将一个字符串写入文件

[b]2,str_replace(array('\r\n'), array("\n"), $str)[/b]


[b]1,strip_tags($v)[/b]
原型:string strip_tags ( string $str [, string $allowable_tags ] )

解读:This function tries to return a string with all NUL bytes, HTML and PHP tags stripped from a given str. It uses the same tag stripping state machine as the fgetss() function.

举例:

$text = '<p>Test paragraph.</p><!-- Comment --> <a href="#fragment">Other text</a>';
echo strip_tags($text);
echo "\n";

// Allow <p> and <a>
echo strip_tags($text, '<p><a>');

输出:
Test paragraph. Other text
<p>Test paragraph.</p> <a href="#fragment">Other text</a>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值