making a php concordancer

Discussion in 'PHP' started by Mr.Samsa, Dec 6, 2007.

  1. #1
    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.
     
    Mr.Samsa, Dec 6, 2007 IP
  2. steb

    steb Peon

    Messages:
    213
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #2
    echo "$before $word $after";
    Code (markup):
    should do it I think? (if not, your substr calls arent doing what they should be)
     
    steb, Dec 6, 2007 IP