A.TXT内容如下
01:富裕
02:一般
03:贫穷
C.TXT内容如下
01:中国
02:美国
03:德国
04:法国
05:葡萄牙
php文件中
$a1=02
$a2=01
$a3=03
$c1=01
$c2=03
$c3=05
请问如何将a开头的变量都替换成A.TXT中的富裕程度,将c开头的变量都替换成国家名字?
回复讨论(解决方案)
file_get_contents函数读取txt内容
首先explode("\r\n", $txt)分行
遍历上一步结果,再用explode拆冒号
得到一个数组:array('01' => '富裕', '02' => '一般')
剩下就好办了
这个思路我考虑过,给这么多分的原因是想要一个很优化的方案,因为配置文件多且大,每个变量要遍历一遍,速度很慢。
$a1 = '02';$a2 = '01';$a3 = '03';$c1 = '01';$c2 = '03';$c3 = '05';$na = 'a';parse_str(str_replace(array(':', PHP_EOL), array('=', '&'), file_get_contents("data/$na.txt")), $ar);foreach(preg_grep("/^$na\d+/", array_keys(get_defined_vars())) as $v) { $$v = $ar[$$v];}$na = 'c';parse_str(str_replace(array(':', PHP_EOL), array('=', '&'), file_get_contents("data/$na.txt")), $ar);foreach(preg_grep("/^$na\d+/", array_keys(get_defined_vars())) as $v) { $$v = $ar[$$v];}echo $a1, PHP_EOL;echo $a2, PHP_EOL;echo $a3, PHP_EOL;echo $c1, PHP_EOL;echo $c2, PHP_EOL;echo $c3, PHP_EOL;一般
富裕
贫穷
中国
德国
葡萄牙
你的方案就是错的,不可能获得良好的性能
想要优化?
将txt中的原格式转换到易用的php数组后,用内置的序列化函数生成字符串再保存回去
这样后面省去了解析的过程
但这个东西重点在于大文件的IO上,要么换mysql,要么用memcache

ThesecrettokeepingaPHP-poweredwebsiterunningsmoothlyunderheavyloadinvolvesseveralkeystrategies:1)ImplementopcodecachingwithOPcachetoreducescriptexecutiontime,2)UsedatabasequerycachingwithRedistolessendatabaseload,3)LeverageCDNslikeCloudflareforservin

You should care about DependencyInjection(DI) because it makes your code clearer and easier to maintain. 1) DI makes it more modular by decoupling classes, 2) improves the convenience of testing and code flexibility, 3) Use DI containers to manage complex dependencies, but pay attention to performance impact and circular dependencies, 4) The best practice is to rely on abstract interfaces to achieve loose coupling.

Yes,optimizingaPHPapplicationispossibleandessential.1)ImplementcachingusingAPCutoreducedatabaseload.2)Optimizedatabaseswithindexing,efficientqueries,andconnectionpooling.3)Enhancecodewithbuilt-infunctions,avoidingglobalvariables,andusingopcodecaching

ThekeystrategiestosignificantlyboostPHPapplicationperformanceare:1)UseopcodecachinglikeOPcachetoreduceexecutiontime,2)Optimizedatabaseinteractionswithpreparedstatementsandproperindexing,3)ConfigurewebserverslikeNginxwithPHP-FPMforbetterperformance,4)

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Zend Studio 13.0.1
Powerful PHP integrated development environment

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Dreamweaver Mac version
Visual web development tools
