欢迎访问 生活随笔!

生活随笔

当前位置: 首页 >

php作为文本进行处理,PHP处理文本和爬虫技巧

发布时间:2023/11/27 47 豆豆
生活随笔 收集整理的这篇文章主要介绍了 php作为文本进行处理,PHP处理文本和爬虫技巧 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

用 php 处理注意,explode()拆分字符串,返回给一个变量便是将拆分出的东西依次存进这个变量,返回给一个list(变量1,变量2,……)则是将拆分出的东西分别存到变量1,变量2,……。

结合 array_fliter() 过滤的时候,如果不给回调函数过滤不干净的时候,可以在增加过滤回调函数,排除 "\r\n" 和 原本的 FALSE(不写过滤回调函数的时候默认就是这个FALSE)。

调试可用file_put_contents("/home/search/result", var_export(['time'=> date('Y-m-d H:i:s'),'res'=>$result], true)."\n", FILE_APPEND);

$file=fopen("mission2.txt","r") or exit("Unable to open file!");

$fileCity=fopen("city2.txt","r") or exit("Unable to open file!");

$arr = []; //用不用都行

// 读取文件每一行,直到文件结尾

while(!feof($file))

{

//echo fgets($file). "
";

$arr[] = fgets($file);

}

while(!feof($fileCity))

{

$city[] = fgets($fileCity);

}

//print_r($arr);

foreach($arr as $a){

list($term[]) = explode(" ",$a);

}

echo '
';

print_r($term);

echo '
';

$city = array_filter($city);

print_r($city);

function gl($gg){

if($gg == "\r\n" or $gg == FALSE) //要用\r\n这个才能过滤掉某些windows里面的换行,还需要加上默认的FALSE,不然也可能滤不干净

return 0;

else

return 1;

}

foreach($city as $c){

$cityAlone[] = array_filter(explode("\t",$c),"gl"); //array_filter不使用第二个参数回调函数的话默认是过滤FALSE

}

echo '
';

echo '

';

print_r($cityAlone);

echo '

';

echo '
';

print_r(array_filter($cityAlone[0]));

foreach($term as $key=>$te){

foreach(array_filter($cityAlone[$key]) as $ci){

echo $ci.$te.'
';

}

}

fclose($fileCity);

fclose($file);

?>

ini_set('memory_limit', '512M');

$fileLeimu = fopen("leimu.txt","r") or exit("Unable to open file!");

$fileCity = fopen("city.txt","r") or exit("Unable to open file!");

$fileZaci1 = fopen("zaci1.txt","r") or exit("Unable to open file!");

while(!feof($fileLeimu))

{

$leimu[] = fgets($fileLeimu);

}

while(!feof($fileCity))

{

$city[] = fgets($fileCity);

}

while(!feof($fileZaci1))

{

$zaci1[] = fgets($fileZaci1);

}

function gl($gg){

if($gg == "\r\n" or $gg == FALSE) //要用\r\n这个才能过滤掉某些windows里面的换行,还需要加上默认的FALSE,不然也可能滤不干净

return 0;

else

return 1;

}

$leimu = array_filter($leimu,"gl");

$city = array_filter($city,"gl");

$zaci1 = array_filter($zaci1,"gl");

$zaci2 = ['上门','找'];

$leimu = array_unique($leimu); //去重

$city = array_unique($city);

$leimu = str_replace(array("\r\n", "\r", "\n"), "", $leimu); //去掉每个字符串元素末尾的换行

$city = str_replace(array("\r\n", "\r", "\n"), "", $city);

$zaci1 = str_replace(array("\r\n", "\r", "\n"), "", $zaci1);

// echo '

';

// print_r($zaci2);

// print_r($leimu);

// print_r($city);

// print_r($zaci1);

// echo '

';

/*

foreach($city as $c){

foreach($leimu as $l){

foreach($zaci1 as $zOne){

$combRes[] = $c.$l.$zOne;

}

}

}

foreach($city as $c){

foreach($leimu as $l){

foreach($zaci2 as $zTwo){

$combRes[] = $c.$zTwo.$l;

}

}

}

foreach($city as $c){

foreach($leimu as $l){

$combRes[] = $c.$l;

}

}

*/

foreach($leimu as $l){

foreach($zaci1 as $zOne){

$combRes[] = $l.$zOne;

}

}

foreach($zaci2 as $zTwo){

foreach($leimu as $l){

$combRes[] = $zTwo.$l;

}

}

foreach($leimu as $l){

$combRes[] = $l;

}

$combRes = implode("\r\n", $combRes); //想要打印到txt中,能有换行的效果,需要添加这个

file_put_contents("456.txt",$combRes);

// echo gettype($combRes);

echo '

';

print_r($combRes);

echo '

';

fclose($fileLeimu);

fclose($fileCity);

fclose($fileZaci1);

获取页面某标签中的内容,若是能借助下载simplehtmldom类打开操作的话,dom方便。若打不开,则用file_get_contents或者curl(网上说效率更高),读取全文内容,然后用正则匹配来做。

require_once "../classes/simplehtmldom_1_5/simple_html_dom.php";

$mainHtml = 'http://***/index.xml';

//$mainHtml ='http://***/20161222-0.xml'; //为何没法用file_get_html打开?

$html = file_get_html($mainHtml); //创建一个DOM

foreach($html->find('loc') as $loc){

$locTextRecord[] = $loc->plaintext;

}

// $htmltest = file_get_contents($locTextRecord[4]);

foreach($locTextRecord as $everyLoc){

$htmltest = file_get_contents($everyLoc);

$reg = '/\(.*?)\/is';

if(preg_match_all($reg, $htmltest, $arr)) {

foreach($arr[1] as $a){

$record[] = $a;

}

} else {

echo "匹配失败!
";

}

}

$record = array_unique($record);

foreach($record as $r){

var_dump($r); //利用 var_dump 来查看变量类型,可以调试和直接在网页上复制用。

}

$html->clear();

?>

一个主xml下含很多子标签,在所有xml中,查找某个字符串:

$mainHtml = file_get_contents('http://***/tp_index.xml');

$reg = '/\(.*?)\/is';

if(preg_match_all($reg, $mainHtml, $arr)) {

foreach($arr[1] as $a){

$locTextRecord[] = $a;

}

} else {

echo "loc匹配失败!
";

}

//var_dump($locTextRecord);

foreach($locTextRecord as $everyLoc){

$htmlTest = file_get_contents($everyLoc);

//$aim = 'https://baidu.com?srcid%3D1000%26id%3D10034_5467_%E5%8C%97%E4%BA%AC';

$aim = '搬家';

if(strpos($htmlTest, $aim)) {

$record[] = $everyLoc;

} else {

echo "aim匹配失败!
";

}

}

//$record = array_unique($record);

foreach($record as $r){

var_dump($r); //利用 var_dump 来查看变量类型,可以调试和直接在网页上复制用。

}

?>

注意:

preg_match()的第三个参数:如果提供了参数matches,它将被填充为搜索结果。 $matches[0]将包含完整模式匹配到的文本, $matches[1] 将包含第一个捕获子组匹配到的文本,以此类推。

preg_match_all()的第三个参数:多维数组,作为输出参数输出所有匹配结果, 数组排序通过flags指定。

总结

以上是生活随笔为你收集整理的php作为文本进行处理,PHP处理文本和爬虫技巧的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。