因为是随机位置,也有可能插入到
这种地方,这个问题该怎么解决?
能否先剥离HTML标签,随机插入后,再还原呢?希望大家帮帮我。
回复讨论(解决方案)
utf-8下测试没问题
$str = <<<EOF测试,测试测试测试测试测试测试测<p class="test">试测试测试,测试测试测试测试测试测试测试,测试测试测试,测试测</p>试测试测试测试测试测试测试测试测试测试,测试测试测试测试测试测试测试1测试,测试1测试测试1测试测试1测试测试1测试,测试1测试测试1测试测试1测试,测试1测试测试1测试EOF; $insert=array('<a href="javascript:;">链接1</a>','<a href="javascript:;">链接2</a>','<a href="javascript:;">链接3</a>');echo randomInsert($str,$insert);function randomInsert($txt,$insert){ preg_match_all("/[\x01-\x7f]|[\xe0-\xef][\x80-\xbf]{2}/", $txt, $match); $delay=array(); $add=0; foreach($match[0] as $k=>$v){ if($v=='<') $add=1; if($add==1) $delay[]=$k; if($v=='>') $add=0; } $str_arr=$match[0]; $len=count($str_arr); foreach($insert as $k=>$v){ $insertk=insertK($len-1,$delay); $str_arr[$insertk].=$insert[$k]; } echo join('',$str_arr);}function insertK($count,$delay){ $insertk=rand(0,$count); if(in_array($insertk,$delay)){ $insertk=insertK($count,$delay); } return $insertk;}
要有注释就好了
好吧,注释
function randomInsert($txt,$insert){//txt 内容;insert要插入的关键字,可以是链接,数组 //将内容拆分成数组,每个字符都是一个value,英文,中文,符号都算一个,只能在utf-8下中文才能拆分 preg_match_all("/[\x01-\x7f]|[\xe0-\xef][\x80-\xbf]{2}/", $txt, $match); $delay=array(); $add=0; //获取不能插入的位置索引号($delay 数组),也就是< > 之间的位置 foreach($match[0] as $k=>$v){ if($v=='<') $add=1; if($add==1) $delay[]=$k; if($v=='>') $add=0; } $str_arr=$match[0]; $len=count($str_arr); foreach($insert as $k=>$v){ //获取随机插入的位置索引值 $insertk=insertK($len-1,$delay); //循环将insert数据 拼接到 随机生成的索引 $str_arr[$insertk].=$insert[$k]; } //合并插入 关键词后的数据,拼接成一段内容 echo join('',$str_arr);}function insertK($count,$delay){//count 随机索引值范围,也就是内容拆分成数组后的总长度-1;delay 不允许的随机索引值,也就是不能在 < > 之间 $insertk=rand(0,$count); if(in_array($insertk,$delay)){//索引值不能在 不允许的位置处(也就是< > 之内的索引值) $insertk=insertK($count,$delay);//递归调用,直到随机插入的索引值不在 < > 这个索引值数组中 } return $insertk;}

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)

phpisusedforsenderemailsduetoitsbuilt-inmail()函数andsupportiveLibrariesLikePhpMailerandSwiftMailer.1)usethemail()functionforbasicemails,butithasimails.2)butithasimimitations.2)

PHP性能瓶颈可以通过以下步骤解决:1)使用Xdebug或Blackfire进行性能分析,找出问题所在;2)优化数据库查询并使用缓存,如APCu;3)使用array_filter等高效函数优化数组操作;4)配置OPcache进行字节码缓存;5)优化前端,如减少HTTP请求和优化图片;6)持续监控和优化性能。通过这些方法,可以显着提升PHP应用的性能。

依赖性注射(DI)InphpisadesignPatternthatManages和ReducesClassDeptions,增强量产生性,可验证性和Maintainability.itallowspasspassingDepentenciesLikEdenceSeconnectionSeconnectionStoclasseconnectionStoclasseSasasasasareTers,interitationApertatingAeseritatingEaseTestingEasingEaseTeStingEasingAndScalability。

cachingimprovesphpermenceByStorcyResultSofComputationsorqucrouctationsorquctationsorquickretrieval,reducingServerLoadAndenHancingResponsetimes.feftectivestrategiesinclude:1)opcodecaching,whereStoresCompiledSinmememorytssinmemorytoskipcompliation; 2)datacaching datacachingsingMemccachingmcachingmcachings


热AI工具

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

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

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

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

热门文章

热工具

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

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

MinGW - 适用于 Windows 的极简 GNU
这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

SublimeText3 英文版
推荐:为Win版本,支持代码提示!

WebStorm Mac版
好用的JavaScript开发工具