I want to use PHP to search through a given URL to find a search term, then display the results with some context surrounding each instance of the word that it found. It's for a class, and I'm new to PHP so I apologize for lack of knowledge. Here is what I have so far: $contents = file_get_contents("http://mcmaster.ca/"); // grab contents $word = "McMaster"; // define word to search // match McMaster with word boundaries (\b) and capture offsets preg_match("/\b$word\b/", $contents); $next = strpos($contents, $char, $offsets); $after = substr($contents, ($offsets+strlen($word)), strlen($contents)-$next); # find string before offset $beforetext = substr($contents, 0, $offsets); $last = strrpos($contents, $char); $before = substr($contents, $last, $offsets-$last); It appears to be finding the word, but I'm not sure how to make the results display in any coherent way.
echo "$before $word $after"; Code (markup): should do it I think? (if not, your substr calls arent doing what they should be)