Форум АНТИЧАТ

Форум АНТИЧАТ (https://forum.antichat.xyz/index.php)
-   PHP, PERL, MySQL, JavaScript (https://forum.antichat.xyz/forumdisplay.php?f=37)
-   -   Нужен простенький скрипт (https://forum.antichat.xyz/showthread.php?t=110012)

iSeoBot 09.03.2009 02:11

Нужен простенький скрипт
 
Как организовать такое
есть два файла file1.txt и file2.txt c содержимым

file1.txt

Цитата:

keyword
keyword2
keyword3
file2.txt

Цитата:

<a href="http://site.com">keyword</a>
<a href="http://site.com">keyword2</a>
<a href="http://site.com">keyword3</a>
нужно зделать так чтоб скрипт брал строку из file1.txt и смотрел нет ли такого кейворда с троке файла file2.txt,если есть сохранить всю строку в file3.txt
и так пока не кончатся кейворды в файле file1.txt

Очень нужно :) Поставлю + и скину чисто символически вм :rolleyes:

Pashkela 09.03.2009 02:51

PHP код:

<?php
$res 
fopen('tema3.txt','w'); // Результирующий файл
$ipArray array_map("trim"file("tema1.txt")); // Файл с кейвордами
$file2 fopen('tema2.txt','r'); // Файл с ссылками
while (!feof($file2)) {
  
$buf trim(fgets($file2,4096));
  
preg_match('|>(.*)</a>|',$buf,$out);
  if( 
in_array($out[1],$ipArray)) fwrite ($res,$buf "\r\n");

fclose ($res);
fclose ($file2);
?>

или так:

PHP код:

<?php
$res 
fopen('tema3.txt','w'); // Результирующий файл
$ipArray array_map("trim"file("tema1.txt")); // Файл с кейвордами
$file2 fopen('tema2.txt','r'); // Файл с ссылками
while (!feof($file2)) {
  
$buf trim(fgets($file2,4096));
  for (
$i=0;$i<count($ipArray);$i++){
    if(
strpos($buf,$ipArray[$i])) {fwrite ($res,$buf "\r\n");break;}
  }

fclose ($res);
fclose ($file2);
echo 
'done';
?>

или так (более правильно, не зависит от размеров памяти и размеров обоих файлов):

PHP код:

<?php

function check($str,$file1) {
 while (!
feof($file1)) {
   
$f =  trim(fgets($file1,4096));
   if (
strpos($str,$f)) return true;
 }
 return 
false;  
}



$res fopen('tema3.txt','w'); // Результирующий файл
$file2 fopen('tema2.txt','r'); // Файл с ссылками
$file fopen('tema1.txt','r'); // Файл с кейвордами
while (!feof($file2)) {
  
$buf trim(fgets($file2,4096));
  if(
check($buf,$file)) fwrite ($res,$buf "\r\n");

fclose ($res);
fclose ($file2);
fclose($file);
echo 
'done';
?>

Плюс и мани можешь оставить себе:)

iSeoBot 09.03.2009 03:10

Спасибо большое!


Время: 05:40