Simple little str_replace trouble.

Discussion in 'PHP' started by Nintendo, Nov 19, 2009.

  1. #1
    In the .html template file, I've created a {title2} to add in the links, the same as the title of the page, but I wanna do some search and replaces so it's URL friendly. In the script, I can't get it at all to do search and replaces to it. The code

    $file_show=str_replace("{title2}",$line['title'],$file_show);

    makes {title2} work through the template, but stuff like

    $title2=str_replace(" ","-",$title2);

    does nothing to replace for example a space to a -. I'm not sure if the codes wrong, or if I got it in the wrong part of the script.

    Complete script. Scroll down to the red to see it.
     
    Nintendo, Nov 19, 2009 IP
  2. Colbyt

    Colbyt Notable Member

    Messages:
    3,224
    Likes Received:
    185
    Best Answers:
    0
    Trophy Points:
    210
    #2
    Hey I owe you a couple for that mod-rewrite article.

    I can't follow your code tonight but am working on something very close to it. The order in which you do the str_replace can have a major affect on the outcome.

    Here is what I have to date:

    // Clean up user errors that might break the rewritten URL
    // User errors that might occur to mess up the script
    		//remove reserved characters from the title this would result in a broken link
    		//remove period from the end of the title this would result in a broken link
    		//remove whitespace from the end of the title to prevent an ending hyphen
    
    	 $title=$get_info['title'];
    	  $title=str_replace('.',' ',$title);
    	 $title=str_replace('?',' ',$title);
    	 $title=trim ($title); //trims the blank space at the beginning and end of string
    
    $title=str_replace(' ','-',$title);
    echo "<a href=".$get_info['id'] ."/". $title . ".html" . "><small>Read More</small></a>";
    
    
    PHP:
    I still need to add the str_replace for the comma and anything else I have missed.
     
    Colbyt, Nov 19, 2009 IP
  3. ads2help

    ads2help Peon

    Messages:
    2,142
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    0
    #3
    $title2 is undefined.

    I don't know what you are trying to do but I guess this is it?

    
    $file_show=str_replace("{title2}",$line['title'],$file_show);
    
    $file_show=str_replace(" ","-",$file_show);
    PHP:
     
    Last edited: Nov 19, 2009
    ads2help, Nov 19, 2009 IP
  4. pixmania

    pixmania Peon

    Messages:
    229
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I am probable way out on this, but are you trying to create a new <meta title and your looking to removes characters etc..


    
    $title2 = preg_replace('/[^a-z^A-Z^]/', ' ', $title2); // removes odd characters like 1£$%^&&*(.)-_  and replaces with spaces, keeping only letters
    
    PHP:
    If you wish to keep numbers for example, you could try this

    
    $title2 = preg_replace('/[^a-z^A-Z^0-9^]/', ' ', $title2); 
    
    PHP:
    You could possible run that prior to your final output
     
    pixmania, Nov 20, 2009 IP
  5. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #5
    Exactly. I already had the first line in there, which allows me to have the title in the URLs, BUT, that second line is what I can't get to work, the search and replace that changes every space to a - instead of just title2 info.

    I can't get *anything* to search and replace the $title2!!!!!!

    BTW, {title2} is the code that goes in the .html template file.)
     
    Nintendo, Nov 28, 2009 IP
  6. shallowink

    shallowink Well-Known Member

    Messages:
    1,218
    Likes Received:
    64
    Best Answers:
    2
    Trophy Points:
    150
    #6
    uh, don't you just need to reverse the order?

    This replaces {title2} in the Template :
    $file_show=str_replace("{title2}",$line['title'],$file_show);

    This replaces $line['title2']'s value but since its after the above it doesn't do any good.

    $line['title2']=str_replace(" ","-",$line['title2']).' ';

    assumption there is that title2 is really title though since it doesn't appear elsewhere in the file.
     
    shallowink, Nov 28, 2009 IP
  7. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #7
    Look at this page, at the URLs of the links at the titles. It does show the actual title, just from adding that one line using {title2} in the template. Wouldn't that mean title2 is real?

    For {title}, there are only two lines in the whole script about that...

    $file=str_replace("{title}",$line['title'],$file); $ret_title=$line['title'];
    $file_show=str_replace("{title}",$line['title'],$file_show);

    I can't find anything else that would make {title} actually be {title}.

    I then tried messing with {title} to see if I could get str_replace working for it....

    Only {title} has -.

    $line['title2']=str_replace(" ","-",$line['title2']).' ';
    $file_show=str_replace("{title2}",$line['title'],$file_show);
    $line['title']=str_replace(" ","-",$line['title']).' ';
    $file_show=str_replace("{title}",$line['title'],$file_show);


    Both {title} and {title2} have -.

    $line['title']=str_replace(" ","-",$line['title']).' ';
    $file_show=str_replace("{title}",$line['title'],$file_show);
    $line['title2']=str_replace(" ","-",$line['title2']).' ';
    $file_show=str_replace("{title2}",$line['title'],$file_show);
     
    Nintendo, Nov 30, 2009 IP
  8. shallowink

    shallowink Well-Known Member

    Messages:
    1,218
    Likes Received:
    64
    Best Answers:
    2
    Trophy Points:
    150
    #8
    ok it does exist in the template, list_by_tag.html.
    but these are the only lines (1's commented out)

    $file_show=str_replace("{title2}",$line['title'],$file_show);

    //ZZZZZZZZZZZNone work
    //$line['title2']=str_replace(" ","-",$line['title2']).' ';

    and they need to be reversed.

    $line['title2']=str_replace(" ","-",$line['title']).' ';
    $file_show=str_replace("{title2}",$line['title2'],$file_show);


    and if that doesn't work, debug it by echoing out the $line['title2'] before and after.
    echo $line['title'];
    $line['title2']=str_replace(" ","-",$line['title']).' ';
    echo $line['title2'];
    $file_show=str_replace("{title2}",$line['title2'],$file_show);

    and why is there a space appended to $line['title2'] ? that kinda defeats the purpose doesn't it?
     
    shallowink, Nov 30, 2009 IP
    Nintendo likes this.
  9. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #9
    Thanks. I almost got it. I got rid of all the lines and added the two from your post, and then it worked. Now I got...

    $line['title2']=preg_replace('/[^a-z^A-Z^0-9^]/', '-',$line['title']).'';
    $line['title2']=str_replace("--","-",$line['title2']).'';
    $line['title2']=str_replace("--","-",$line['title2']).'';
    $line['title2']=str_replace("--","-",$line['title2']).'';
    $file_show=str_replace("{title2}",$line['title2'],$file_show);

    I just need a code to get rid of the - if it's the last character. For example,

    /Who-s-in-charge-/
    to
    /Who-s-in-charge/
     
    Nintendo, Nov 30, 2009 IP
  10. shallowink

    shallowink Well-Known Member

    Messages:
    1,218
    Likes Received:
    64
    Best Answers:
    2
    Trophy Points:
    150
    #10
    I would just do
    trim($line['title2']) ;

    before the replacements to get rid of whitespace which is what's causing the extra -.
     
    shallowink, Nov 30, 2009 IP
  11. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #11
    Using

    $line['title2'] = trim(preg_replace('/[^\w\d]+/', ' ', $line['title']));
    $line['title2'] = str_replace(' ', '-', $line['title2']);
    $file_show=str_replace("{title2}",$line['title2'],$file_show);

    did it. I had got rid of the whitespace by replacing .' '; with .'';, so only titles that actually ended with a special character had them at the end.
     
    Last edited: Nov 30, 2009
    Nintendo, Nov 30, 2009 IP
  12. shallowink

    shallowink Well-Known Member

    Messages:
    1,218
    Likes Received:
    64
    Best Answers:
    2
    Trophy Points:
    150
    #12
    cool, like the fact you let people search for ads ;)
    didn't work but still funny.
     
    shallowink, Nov 30, 2009 IP