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.
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.
$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:
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
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.)
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.
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);
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?
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/
I would just do trim($line['title2']) ; before the replacements to get rid of whitespace which is what's causing the extra -.
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.