PHP получить источник и поиск по слову - PullRequest
0 голосов
/ 16 января 2009

Мне нужна помощь. Я хочу написать программу, которая ищет слово в исходном коде.

Вот пример на Python:

import urllib2, re

site = "http://stackoverflow.com/"
tosearch = "Questions"
source = urllib2.urlopen(site).read()
if re.search(tosearch,source):
     print "Found The Word", tosearch

Ответы [ 2 ]

7 голосов
/ 16 января 2009
<?php

$site = "http://stackoverflow.com/";
$tosearch = "Questions";
$source = file_get_contents($site);
if(preg_match("/{$tosearch}/", $source)): // fixed, thanks meouw
  echo "Found the the word {$tosearch}";
endif;

?>
1 голос
/ 16 января 2009

CURL - хороший путь:

    $toSearch = 'Questions';

    // create curl resource
    $ch = curl_init();

    // set url
    curl_setopt($ch, CURLOPT_URL, "http://stackoverflow.com/");

    //return the transfer as a string
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    // $output contains the output string
    $output = curl_exec($ch);

    // close curl resource to free up system resources
    curl_close($ch);

    //search for the string
    if(strpos($output, $toSearch ) !== FALSE ) {
       echo "Found The Word " . $toSearch;
    }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...