Little search and replace trouble

Discussion in 'PHP' started by Nintendo, Sep 28, 2010.

  1. #1
    $file=str_replace("{title2}",$line['title'],$file);
    $file['title2'] = trim(preg_replace('/[^\w\d]+/', ' ', $file['title2']));
    $file['title2'] = str_replace(' ', '-', $file['title2']);
    
    PHP:
    The first line does what it should do, have {title2} in the template generate the page title. The next two lines don't do the replacing.
     
    Nintendo, Sep 28, 2010 IP
  2. scylla

    scylla Notable Member

    Messages:
    1,025
    Likes Received:
    33
    Best Answers:
    1
    Trophy Points:
    225
    #2
    You mean with all of that .htaccess stuff you've been posting, you can't whip yourself a solution out of that?
     
    scylla, Oct 9, 2010 IP
  3. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #3
    
    //replace the {title2} placeholder with the actual title...
    $file = str_replace("{title2}", $line['title'], $file);
    
    //replace anything which is not a 'word' character (a-z,0-9,_) with a - (dash)
    $file['title2'] = preg_replace('#[^\w]+#', '-', $file['title2']); //note: the # delimiter does'nt change any functionality (personal preference)
    PHP:
    You don't need the \d within the second line as \w includes that...furthermore the third line is unneccesary as you can simply replace the second line with the - (dash)...instead of adding an extra line of code.

    -----

    That should clear things up for you...if your still getting problems reply with what $file['title2'] contains (by echo'ing it) alongside what would need to be replaced (with what..if anything)
     
    Last edited: Oct 9, 2010
    danx10, Oct 9, 2010 IP