";preg_ replace()”;工作不正常


"preg_replace()" not working properly.

我有一个文本文件,我提取了用http://排序的所有域地址现在我想更换所有的http://。在我的匹配数组中有",但什么都没有发生,我甚至没有得到一个错误

$list = file_get_contents( 'file.txt' );
preg_match_all( "/http:'/'/.([a-z]{1,24}).([a-z^0-9-]{1,23}).([a-z]{1,3})/", $list, $matches );
for ($i=0; $i>=50; $i++) {
    $pattern = array();
    $replacement = array();
    $pattern[0][$i] = "/http:'/'/.[w-w]{1,3}/";
    $replacement[0][$i] = '';
    preg_replace( $pattern[0][$i], $replacement[0][$i], $matches[0][$i] );
}
print_r($matches);

您的循环永远不会运行,因为0 >= 50会产生false。也就是说,你正在寻找的是一个地图操作:

$matches = array_map(function($match) {
    return preg_replace('~^http://w{1,3}~', '', $match);
}, $matches[0]);
print_r($matches);

preg_match_all也有问题。正则表达式中的句点与任何字符匹配。

$list = file_get_contents( 'file.txt' );
preg_match_all( "/http:'/'/([a-z]{1,24})'.([a-z^0-9-]{1,23})'.([a-z]{1,3})/", $list, $matches );
$pattern = "/http:'/'/(.[w-w]{1,3})/";
$replacement = '$1';
$matches[0] = preg_replace( $pattern, $replacement, $matches[0] );
print_r($matches);