当前位置: 首页 > 后端技术 > PHP

PHP常用方法总结

时间:2023-03-30 02:27:50 PHP

总结了工作中常用的方法,以供后续搜索。";}打印_r($数据);回声“”;如果($is_exit)退出();}}//1.获取根目录,从后往前推define('ROOT_PATH',substr(__FILE__,0,strlen(__FILE__)-17));//应用根目录rtrim'test/fun_test.php'dump(ROOT_PATH);//print/Applications/XAMPP/xamppfiles/htdocs/web/camel/shop_wms///2.设置include_pathset_include_path(get_include_path().PATH_SEPARATOR.ROOT_PATH);//设置包含路径,包含文件可以忽略ROOT_PATH部分//3.定义分隔符常量define('DS',DIRECTORY_SEPARATOR);//4.根据文件名获取类名和扩展名dump("========4.根据文件名获取类名和扩展名============");$impl_file="/web/camel/shop_wms/test/hello.class.php";list($impl_class,$ext)=explode('.',basename($impl_file),2);//arrayexplode(string$delimiter,string$string[,int$limit])//如果设置了limit参数并且为正数,则返回的数组至多包含限制元素,最后一个元素将包含字符串的其余部分转储($impl_class);//打印:hellodump($ext);//print:class.php//5.标准对象dump("========5.标准对象=============");$objitem=newstdClass();$objitem->file="test/hello.class.php";//属的动态分配$objitem->class="hello";dump($objitem);//6.从路径解析参数dump("========6.从路径解析参数============");if(DS=='\\'){//$app_script_file=str_replace('/',DS,$_SERVER['SCRIPT_FILENAME']);}else{$app_script_file=$_SERVER['SCRIPT_FILENAME'];}dump($app_script_file);//print:/Applications/XAMPP/xamppfiles/htdocs/web/camel/shop_wms/test/fun_test.php//把$app_script_file写死在这里:$app_script_file="/Applications/XAMPP/xamppfiles/htdocs/web/camel/shop_test/shop_api/boot/req_init.php";$cnt=strlen(ROOT_PATH);$file_path=substr($app_script_file,$cnt,strlen($app_script_file)-$cnt);list($app_name,$other)=爆炸(DS,$file_path,2);转储($app_name);//测试转储($其他);//fun_test.php//D:/xampp/php/php.exe-fD:/xampp/htdocs/shop/shop_api/webservice/web/index.phpapp_fmt=jsonapp_act=taobao_api/taobao_trades_sold_get_allsd_id=4start_modified="2012-09-0100:00:00"end_modified="2012-09-1100:00:00"$cnt=strlen('web'.DS.'app'.DS);///etast/shop_api/webservice/web/app/taobao_api.php//7.路径数据过滤dump("========7.路径数据过滤============");$pathgrp="/women/hek8dso*jid\/heloid.php";$pathgrp=preg_replace('/[^a-z0-9_\/]+/i','',$pathgrp);转储($pathgrp);///women/hek8dsojid/heloidphp$rpos=strrpos($pathgrp,'/');转储($rpos);//17if($rpos!==false){$path=substr($pathgrp,0,++$rpos);转储($rpos);转储($路径);///women/hek8dsojid/$grp=substr($pathgrp,$rpos,strlen($pathgrp)-$rpos);}else$grp=$pathgrp;dump($grp);//heloidphp//8.用户自定义错误处理函数dump("========8.用户自定义错误处理函数============");//User-定义错误处理函数functionmyErrorHandler($errno,$errstr,$errfile,$errline){echo"Customerror:[$errno]$errstr
";echo"$errfile中$errline行出错
";}//设置用户自定义错误处理函数set_error_handler("myErrorHandler");$test=2;//触发错误if($test>1){trigger_error("自定义错误已被触发");}//打印:/**自定义错误:[1024]已触发自定义错误/Applications/XAMPP/xamppfiles/htdocs/web/camel/shop_wms/test/fun_test.php中第105行的错误*///9.解析路径数据转储("========9.解析路径数据==============");functionget_path_grp_act($action,&$path,&$grp,&$act){if(!$action)return;$action=str_replace('\\','/',$action);//如果是openapi接口if(strpos($action,'efast')===0){$action=preg_replace('/[^a-z0-9_\.]+/i','',$action);$action=str_replace('..','.',$action);$action=preg_replace('/\./','/',$action,1);$action=str_replace('.','_',$action);}else{$action=preg_replace('/[^a-z0-9_\/]+/i','',$action);}$path=$grp=NULL;$rpos=strrpos($action,'/');如果($rpos!==false){$pathgrp=substr($action,0,$rpos);$act=substr($action,$rpos+1,strlen($action)-$rpos);$rpos=strrpos($pathgrp,'/');如果($rpos!==false){$path=substr($pathgrp,0,++$rpos);$grp=substr($pathgrp,$rpos,strlen($pathgrp)-$rpos);}else$grp=$pathgrp;}else$act=$action;}$path=$grp=$act=NULL;$action='taobao_api/item_quantity_sync';get_path_grp_act($action,$path,$grp,$act);echo'path='.$path;回声“
”;echo'grp='.$grp;回声“
”;echo'act='.$act;echo"
";结果打印:path=
grp=taobao_api
act=item_quantity_sync
//10.调用POST方法//shop_test/moudle/openapi/OpenAPIOperatingBase.php