regular expression replace <title>...</title> in fread($htmlfile)

Discussion in 'PHP' started by ljrweb, May 4, 2009.

  1. #1
    Hi all.. im racking my brain here and its late so maybe im missing something.

    im reading an htdoc using fopen/fread into a string var $contents

    i want to replace the entire <title> meta tag from start to end..

    $file = "index.php";

    $h = fopen($file, "r");

    $contents = fread($h, filesize($file));

    fclose($h);

    eregi_replace("<title>[a-zA-Z0-9._-]+</title>","<title>new title tag</title>",$contents);

    nothings happening... is it possible that its gettings stopped by other characters in the pattern im not expecting or is there an inherent error-- im new to reg ex :)

    thanks

    Logan
     
    ljrweb, May 4, 2009 IP
  2. Barti1987

    Barti1987 Well-Known Member

    Messages:
    2,703
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    185
    #2
    Barti1987, May 4, 2009 IP
  3. Ristaki

    Ristaki Peon

    Messages:
    31
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    In regular expressions you should always escape the meta characters. Sometimes you forget some, so i advise you to use this simple function:
    $pattern = ''; // the regexp that you want to use
    $pattern = addcslashes($pattern, '^[.${*(\\+)|?<>');
     
    Ristaki, May 4, 2009 IP
  4. ljrweb

    ljrweb Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    thanks guys-- workin fine now.
     
    ljrweb, May 4, 2009 IP