对工作中常用的方法进行小结,以便后续查找。
"; print_r($data); echo ""; if($is_exit) exit(); }}// 1.获取根目录,由后往前推define('ROOT_PATH',substr(__FILE__,0,strlen(__FILE__)-17)); // 应用根目录 rtrim 'test/fun_test.php'dump(ROOT_PATH); // 打印 /Applications/XAMPP/xamppfiles/htdocs/web/camel/shop_wms/// 2.设置include_pathset_include_path(get_include_path(). PATH_SEPARATOR . ROOT_PATH); //设置include 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);// array explode ( string $delimiter , string $string [, int $limit ] )// 如果设置了 limit 参数并且是正数,则返回的数组包含最多 limit 个元素,而最后那个元素将包含 string 的剩余部分。dump($impl_class); // 打印:hellodump($ext); // 打印:class.php// 5.标准对象dump("======== 5.标准对象 ============");$objitem = new stdClass();$objitem->file = "test/hello.class.php"; // 动态的分配属相$objitem->class = "hello";dump($objitem);// 6.从路径解析参数dump("======== 6.从路径解析参数 ============");if(DS=='\\'){ // windows 环境下 $app_script_file = str_replace('/', DS, $_SERVER['SCRIPT_FILENAME']);}else{ $app_script_file = $_SERVER['SCRIPT_FILENAME'];}dump($app_script_file); // 打印:/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) = explode(DS, $file_path, 2);dump($app_name); // testdump($other); // fun_test.php// D:/xampp/php/php.exe -f D:/xampp/htdocs/shop/shop_api/webservice/web/index.php app_fmt=json app_act=taobao_api/taobao_trades_sold_get_all sd_id=4 start_modified="2012-09-01 00:00:00" end_modified="2012-09-11 00: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);dump($pathgrp); // /women/hek8dsojid/heloidphp$rpos=strrpos($pathgrp,'/');dump($rpos); // 17if($rpos!==false){ $path=substr($pathgrp, 0, ++$rpos); dump($rpos); dump($path); // /women/hek8dsojid/ $grp=substr($pathgrp,$rpos,strlen($pathgrp)-$rpos);} else $grp=$pathgrp;dump($grp); // heloidphp// 8.用户定义的错误处理函数dump("======== 8.用户定义的错误处理函数 ============");// 用户定义的错误处理函数function myErrorHandler($errno, $errstr, $errfile, $errline) { echo "Custom error: [$errno] $errstr"; echo " Error on line $errline in $errfile";}// 设置用户定义的错误处理函数set_error_handler("myErrorHandler");$test=2;// 触发错误if ($test>1) { trigger_error("A custom error has been triggered");}// 打印:/** Custom error: [1024] A custom error has been triggered Error on line 105 in /Applications/XAMPP/xamppfiles/htdocs/web/camel/shop_wms/test/fun_test.php */// 9.解析路径数据dump("======== 9.解析路径数据 ============"); function get_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,'/'); if($rpos!==false){ $pathgrp=substr($action,0,$rpos); $act=substr($action,$rpos+1,strlen($action)-$rpos); $rpos=strrpos($pathgrp,'/'); if($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 ""; echo 'grp='.$grp; echo ""; echo 'act='.$act; echo "";结果打印:path=grp=taobao_apiact=item_quantity_sync// 10.调用 POST 方法// shop_test/moudle/openapi/OpenAPIOperatingBase.php