Php 想要检测多个URL时,preg_match不起作用
我想自动检测字符串中的任何链接,并用Php 想要检测多个URL时,preg_match不起作用,php,url,preg-replace,preg-match,Php,Url,Preg Replace,Preg Match,我想自动检测字符串中的任何链接,并用[链接索引]替换它们。例如,如果我有一个字符串,如testhttps://www.google.com/ 嗯http://stackoverflow.com/结果将是测试[0]毫米小时[1] 现在我试着用这个 $reg_exUrl = '/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i'; if(preg_match($reg_ex
[链接索引]
替换它们。例如,如果我有一个字符串,如testhttps://www.google.com/ 嗯http://stackoverflow.com/
结果将是测试[0]毫米小时[1]
现在我试着用这个
$reg_exUrl = '/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i';
if(preg_match($reg_exUrl, $_POST['commento'], $url)) {
for ($i = 0; $i < count($url); $i++) {
$_POST['commento'] = preg_replace($reg_exUrl, "[" . $i . "]", $_POST['commento']);
}
}
$reg|u exUrl='/\b(?:(?:https?| ftp):\/\/\/\/\/\ www\.)[-a-z0-9+&\/%?=~!:,.;]*[-a-z0-9+&\/%=~/i';
if(preg_match($reg_exUrl,$_POST['commento'],$url)){
对于($i=0;$i
但是我一直得到
test[0]mmh[0]
,如果我尝试var\u转储(count($url))
结果总是得到1。如何修复此问题?因此,更好的解决方案是将传入字符串拆分为每个url
段之间的字符串数组,然后在连续的非url组件之间插入[$i]
# better solution, perform a split.
function process_line2($input) {
$regex_url = '/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i';
# split the incoming string into an array of non-url segments
# preg_split does not trim leading or trailing empty segments
$non_url_segments = preg_split($regex_url, $input, -1);
# inside the array, combine each successive non-url segment
# with the next index
$out = [];
$count = count($non_url_segments);
for ($i = 0; $i < $count; $i++) {
# add the segment
array_push($out, $non_url_segments[$i]);
# add its index surrounded by brackets on all segments but the last one
if ($i < $count -1) {
array_push($out, '[' . $i . ']');
}
}
# join strings with no whitespace
return implode('', $out);
}
下面是一个合并了修复程序的示例
<?php
# original function
function process_line($input) {
$reg_exUrl = '/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i';
if(preg_match($reg_exUrl, $input, $url)) {
for ($i = 0; $i < count($url); $i++) {
$input = preg_replace($reg_exUrl, "[" . $i . "]", $input);
}
}
return $input;
}
# function with fixes incorporated
function process_line1($input) {
$reg_exUrl = '/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i';
if(preg_match_all($reg_exUrl, $input, $url)) {
$url_matches = $url[0];
for ($i = 0; $i < count($url_matches); $i++) {
echo $i;
# add explicit limit of 1 to arguments of preg_replace
$input = preg_replace($reg_exUrl, "[" . $i . "]", $input, 1);
}
}
return $input;
}
$input = "test https://www.google.com/ mmh http://stackoverflow.com/";
$input = process_line1($input);
echo $input;
?>
<?php
# original function
function process_line($input) {
$reg_exUrl = '/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i';
if(preg_match($reg_exUrl, $input, $url)) {
for ($i = 0; $i < count($url); $i++) {
$input = preg_replace($reg_exUrl, "[" . $i . "]", $input);
}
}
return $input;
}
# function with fixes incorporated
function process_line1($input) {
$reg_exUrl = '/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i';
if(preg_match_all($reg_exUrl, $input, $url)) {
$url_matches = $url[0];
for ($i = 0; $i < count($url_matches); $i++) {
echo $i;
# add explicit limit of 1 to arguments of preg_replace
$input = preg_replace($reg_exUrl, "[" . $i . "]", $input, 1);
}
}
return $input;
}
$input = "test https://www.google.com/ mmh http://stackoverflow.com/";
$input = process_line1($input);
echo $input;
?>