Php 用订单号替换文本中的链接(并存储在数组中)的最快方法
有一个Php 用订单号替换文本中的链接(并存储在数组中)的最快方法,php,arrays,string,preg-replace,preg-match,Php,Arrays,String,Preg Replace,Preg Match,有一个$str字符串可能包含html文本,包括here hahahaha”; 我们得到: linkArray[0]="<a href='/review/'>review</a>"; positionArray[0] = 10;//position of the first link in the string linkArray[1]="<a class='abc' href='/about/'>link2</a>"; positionArra
$str
字符串可能包含html文本,包括here hahahaha”;
我们得到:
linkArray[0]="<a href='/review/'>review</a>";
positionArray[0] = 10;//position of the first link in the string
linkArray[1]="<a class='abc' href='/about/'>link2</a>";
positionArray[1]=45;//position of the second link in the string
$changedStr="some text [[0]] here [[1]] hahaha";
linkArray[0]=“”;
positionArray[0]=10;//字符串中第一个链接的位置
链接数组[1]=“”;
positionArray[1]=45;//字符串中第二个链接的位置
$changedStr=“一些文本[[0]]在这里[[1]]哈哈哈”;
有没有比使用
for
遍历整个字符串更快的方法(性能)?我会使用正则表达式来实现这一点,请检查:
请在此处尝试:
并使用以下命令:
找到最好的正则表达式来解决您可能发现的每种情况:preg_match_all,如果您正确设置模式,将返回一个包含您所需的每个链接的数组
编辑:
在您的情况下,假设您想保留“
llkdla
使用上述regexp进行输出:
Array
(
[0] => Array
(
[0] => <a href="ciccio">test</a>
[1] => <a href="caio">Lkdlasdk</a>
[2] => <a href="lol">xx</a>
)
)
数组
(
[0]=>阵列
(
[0] =>
[1] =>
[2] =>
)
)
希望这有帮助我会使用正则表达式来执行此操作,请检查以下内容: 请在此处尝试: 并使用以下命令: 找到最好的正则表达式来解决您可能发现的每种情况:preg_match_all,如果您正确设置模式,将返回一个包含您所需的每个链接的数组 编辑: 在您的情况下,假设您想保留“
llkdla
使用上述regexp进行输出:
Array
(
[0] => Array
(
[0] => <a href="ciccio">test</a>
[1] => <a href="caio">Lkdlasdk</a>
[2] => <a href="lol">xx</a>
)
)
数组
(
[0]=>阵列
(
[0] =>
[1] =>
[2] =>
)
)
希望这有帮助这可以通过preg\u match\u all with preg\u OFFSET\u CAPTURE标志来完成 e、 g 有关更多信息,请单击链接 对于$changedStr: 让$out作为preg_match_all的输出字符串
$count= 0;
foreach($out[0] as $result) {
$temp=preg_quote($result[0],'/');
$temp ="/".$temp."/";
$str =preg_replace($temp, "[[".$count."]]", $str,1);
$count++;
}
var_dump($str);
这将提供以下输出:
string 'some text [[0]] here [[1]] hahaha' (length=33)
这可以通过使用preg_OFFSET_CAPTURE标志进行preg_match_all来完成 e、 g 有关更多信息,请单击链接 对于$changedStr: 让$out作为preg_match_all的输出字符串
$count= 0;
foreach($out[0] as $result) {
$temp=preg_quote($result[0],'/');
$temp ="/".$temp."/";
$str =preg_replace($temp, "[[".$count."]]", $str,1);
$count++;
}
var_dump($str);
这将提供以下输出:
string 'some text [[0]] here [[1]] hahaha' (length=33)
谢谢,但是$changedStr呢?谢谢,但是$changedStr呢?
$count= 0;
foreach($out[0] as $result) {
$temp=preg_quote($result[0],'/');
$temp ="/".$temp."/";
$str =preg_replace($temp, "[[".$count."]]", $str,1);
$count++;
}
var_dump($str);
string 'some text [[0]] here [[1]] hahaha' (length=33)