preg替换不使用file_get_content - 删除括号()&替换单词

时间:2013-08-29 15:09:05

标签: php regex

<?php 
$file = file_get_contents("http://www.mywebsite.net/folder/indexhtml.asp");
preg_match_all('~<td width="23%" height="23" align="right">(.*?)</td>~is',$file, $matches5);
print implode("\n", $matches5[0]);       //This prints out (Page: 889) on website ?>

我正在尝试从名为matches5 variable

的字符串中删除括号和带有“Number”的单词“Page”

每次我在下面使用此代码显示空白,这让我很困惑。请帮助我,因为我是PHP新手 第二个代码

<?php

$file = file_get_contents("http://www.mywebsite.net/folder/indexhtml.asp"); 
preg_match_all('~<td width="23%" height="23" align="right">(.*?)</td>~is',$file,  $matches5); 
$result = preg_replace('/(?<=^| ).(?=$| )/sm', '', $matches5); 
print implode("\n", $result[0]);

?>

1 个答案:

答案 0 :(得分:0)

第一个例子(坏):

$array = array("");
$replace = array("");
// Please put the maximum number this (Page: maximum)
for($i = 1; $i <= 1000; $i++)
{
array_push($array, "(Page: $i)");
// Put your replace string this or leave blank
array_push($replace, "I am replaced :)");
}

$file = 'I am (Page: 129) this is test.';

$new_string = str_replace($array, $replace, $file);

echo $new_string;

第二个例子(好)。

$file = 'I am (Page: 129) this is test.';

$new_string = preg_replace('(Page: ([0-9]+))', "", $file);
$new_string = str_replace("()", "", $new_string);

echo $new_string;