Language
Language

Reputation:

PHP Get Source and Search for Word

I need help i want to code a program that search for a word inside the source code.

Here a Example in 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

Upvotes: 0

Views: 341

Answers (2)

Julio Greff
Julio Greff

Reputation: 135

<?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;

?>

Upvotes: 7

Brian Fisher
Brian Fisher

Reputation: 23989

CURL is a good way to go:

    $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;
    }

Upvotes: 1

Related Questions