PHP中查找文件的相对路径或者绝对路径的工具
以前面试题目中有一题为求两个文件的相对路径,当时觉得没有电脑,书写代码太麻烦,没有做那个题目,今日工作比较闲遐,就想起这事来,完成了两个函数,代码未经过实际生产环境考验,如引用至生产环境,还请慎重阅读,主要是参考学习
查看源代码打印帮助
1 <?php 2 /** 3 * 文件的相对路径或者绝对路径查找工具 4 * 5 * @author 清源教育<support@tsingyuan.cn > 6 * @version $Id:v 1.0 ,2013/10/13 17:29:00 7 * @copyright (c) Copyright;tsingyuan,2013 8 */ 9 10 /** 11 * 返回文件的绝对路径 12 * 13 * @param string $filename 14 * @return string 15 */ 16 function absoluteroute($filename) 17 { 18 $split = '/\/|\\\/'; 19 $currentdir = preg_split($split, dirname(__FILE__)); 20 $dirarr = preg_split($split, $filename); 21 $diracount = count($dirarr); 22 $cda = count($currentdir); 23 if(strpos(PHP_OS, 'WIN') !== false) 24 $reg = '/\w\:/'; 25 else 26 $reg = '/\//'; 27 if(!preg_match($reg, $dirarr[0])) 28 { 29 foreach($dirarr as $nk=>$name) 30 { 31 if($name == "." || $name == '..') 32 { 33 if($name == '..') 34 $filenamearr = array_slice($currentdir, 0, -($nk+1)); 35 if($name == '.' && $nk == '0') 36 $filenamearr = array_slice($currentdir, 0); 37 } 38 else 39 { 40 $filenamearr[] = $name; 41 } 42 } 43 $filename = implode('/', $filenamearr); 44 } 45 return $filename; 46 } 47 48 /** 49 * 返回两个文件的相对路径 (PS:^_^不错的php学习交流群:276167802,验证:csl,有兴趣的话可以加入进来一起讨论) 50 * (为了保证输入的相对路径参数可用,故此函数依赖absoluteroute) 51 * 52 * @param string $filenamea 53 * @param string $filenameb 54 * @return string 55 */ 56 function relativeroute($filenamea, $filenameb) 57 { 58 $split = '/\/|\\\/'; 59 $filenamea = absoluteroute($filenamea); 60 $filenameb = absoluteroute($filenameb); 61 $dira =preg_split($split, $filenamea); 62 $dirb =preg_split($split, $filenameb); 63 $flag = true; 64 if(count($dira) >= count($dirb)) 65 { 66 $tmp = array(); 67 $tmp = $dira; 68 $dira = $dirb; 69 $dirb = $tmp; 70 $flag = false; 71 } 72 foreach($dira as $k=>$v) 73 if($v != $dirb[$k])break; 74 75 $dirr = array_slice($dirb, $k); 76 77 $k == 1 ? $dtag = '/' : $dtag = './'; 78 $result = $dtag.implode('/', $dirr); 79 if(!$flag) 80 { 81 if($dira[0] == '')array_shift($dira); 82 foreach($dirr as $kk=>&$v) 83 ($kk+1) != count($dirr)? 84 ($v ? 85 $v = '..' : 86 $v = '.' 87 ) : 88 $v = implode('/', array_slice($dira, $k)) ; 89 $result = $dtag.implode('/', $dirr); 90 } 91 return $result; 92 }
本文出自 “清源教育” 博客,转载请注明此处,谢谢!

aphpdepentioncontiveContainerIsatoolThatManagesClassDeptions,增强codemodocultion,可验证性和Maintainability.itactsasaceCentralHubForeatingingIndections,因此reducingTightCightTightCoupOulplingIndeSingantInting。

选择DependencyInjection(DI)用于大型应用,ServiceLocator适合小型项目或原型。1)DI通过构造函数注入依赖,提高代码的测试性和模块化。2)ServiceLocator通过中心注册获取服务,方便但可能导致代码耦合度增加。

phpapplicationscanbeoptimizedForsPeedAndeffificeby:1)启用cacheInphp.ini,2)使用preparedStatatementSwithPdoforDatabasequesies,3)3)替换loopswitharray_filtaray_filteraray_maparray_mapfordataprocrocessing,4)conformentnginxasaseproxy,5)

phpemailvalidation invoLvesthreesteps:1)格式化进行regulareXpressecthemailFormat; 2)dnsvalidationtoshethedomainhasavalidmxrecord; 3)

tomakephpapplicationsfaster,关注台词:1)useopcodeCachingLikeLikeLikeLikeLikePachetoStorePreciledScompiledScriptbyTecode.2)MinimimiedAtabaseSqueriSegrieSqueriSegeriSybysequeryCachingandeffeftExting.3)Leveragephp7 leveragephp7 leveragephp7 leveragephpphp7功能forbettercodeefficy.4)

到ImprovephPapplicationspeed,关注台词:1)启用opcodeCachingwithapCutoredUcescriptexecutiontime.2)实现databasequerycachingusingpdotominiminimizedatabasehits.3)usehttp/2tomultiplexrequlexrequestsandredececonnection.4 limitsclection.4.4

依赖注入(DI)通过显式传递依赖关系,显着提升了PHP代码的可测试性。 1)DI解耦类与具体实现,使测试和维护更灵活。 2)三种类型中,构造函数注入明确表达依赖,保持状态一致。 3)使用DI容器管理复杂依赖,提升代码质量和开发效率。

databasequeryOptimizationinphpinvolVolVOLVESEVERSEVERSTRATEMIESOENHANCEPERANCE.1)SELECTONLYNLYNESSERSAYCOLUMNSTORMONTOUMTOUNSOUDSATATATATATATATATATATRANSFER.3)


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

ZendStudio 13.5.1 Mac
功能强大的PHP集成开发环境

SecLists
SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

Dreamweaver Mac版
视觉化网页开发工具

Dreamweaver CS6
视觉化网页开发工具

SublimeText3汉化版
中文版,非常好用