tags:

views:

116

answers:

3

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
+7  A: 
<?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;

?>
Julio Greff
if( preg_match( "/$tosearch/", $source ) ) { //code}
meouw
Actually, preg_match('/'.preg_quote($tosearch, '/').'/', $source) ... or in this case just : is_int(strstr($source, $tosearch))
troelskn
@troelskn in this case, I would use strpos($source, $tosearch) !== false
Julio Greff
you're right .. i got them mixed up - that's what i meant to say with the last part of my previous comment
troelskn
+1  A: 

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;
    }
Brian Fisher
A: 

Helo, Interesting case. How could I get a link from a source code of a page?

I mean I have the page example.com/link.html

Inside the link.html in the html body there is a link that I want to take. How could I do that?

ariel