因为是随机位置,也有可能插入到
这种地方,这个问题该怎么解决?
能否先剥离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)實現databasequerycachingingusingpdotominiminimizedatabasehits.3)usehttp/2tomultiplexrequlexrequestsandreduceconnection.4 limitesclection.4.4

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

DatabasequeryoptimizationinPHPinvolvesseveralstrategiestoenhanceperformance.1)Selectonlynecessarycolumnstoreducedatatransfer.2)Useindexingtospeedupdataretrieval.3)Implementquerycachingtostoreresultsoffrequentqueries.4)Utilizepreparedstatementsforeffi

phpisusedforsenderemailsduetoitsbuilt-inmail()函數andsupportivelibrariesLikePhpMailerAndSwiftMailer.1)usethemail()functionForbasiceMails,butithasimails.2)butithasimail.2)

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

依賴性注射(DI)InphpisadesignPatternthatManages和ReducesClassDeptions,增強量強制性,可驗證性和MATIALWINABIOS.ItallowSpasspassingDepentenciesLikEdenciesLikedAbaseConnectionStoclasseconnectionStoclasseSasasasasareTers,interitationAseTestingEaseTestingEaseTestingEaseTestingEasingAndScalability。

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 - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!

WebStorm Mac版
好用的JavaScript開發工具