php for some special characters

Status
Not open for further replies.

Icecube

Up 24h/day
Mar 14, 2007
1,169
9
0
Europe
hello guys

I'm coding a site that will use mod rewrite to send the users to the real php generated page ( say search.php?id=123456) but I'll use mod_rewrite to make the url look like /somecategory/123456-TheNameOfThatThing.html

now the database I'm using happens to have any kind of crap in TheNameOfThatThing part including " * ' ] etc

is there a php function that I can use o "make the string clean"?
remember I don't car if the string is fucked and some chars are removed (I actually want to remove them), I won't use it to run the query, I'll use just the number

any idea?
or the only way to go is the good old str_replace used for all those characters?
 


Code:
function seo_url ($title){  
    $address = $title;  
    $address = strtr ($address, "ÁÄČÇĎÉĚËÍŇÓÖŘŠŤÚŮÜÝŽáäčçďéěëíňóöřšťúůüýž ", "AACCDEEEINOORSTUUUYZaaccdeeeinoorstuuuyz-");  
    $address = strtolower ($address);   
    $re = "/[^[:alpha:][:digit:]]/";  
    $replacement = "-";  
    $address = preg_replace ($re, $replacement, $address);  
    $address = trim ($address, "-");  
    $re = "/[-]+/";  
    $replacement = "-";  
    $address = preg_replace ($re, $replacement, $address);  
    return $address;  
}
hope it helps
 
  • Like
Reactions: Icecube
he's demonstrating a preg_replace. A cursory reading looks like he's leaving in the numbers and the letters. I think you only want the numbers, so leave out the [:alpha:] one.

basically "change everything that's not a digit to ''. "
 
Wait, now I'm confused. I reread his function and it's doing something weird with the hyphens.

I'm assuming you have
123456-someotherstuff
and you want
123456

this preg_replace will fix that.

$input = "123456-something";

print "$input\n";

print preg_replace( "|[^\d]|", '', $input )."\n";
 
no, as I said in the first post I have something like 123456-Wireless*Mouse'Cool] and I want to remove all that crap, I don't care what's the output string: I'll explode it and use just the starting 123456 in my script, it's exclusively for SEO purposes
 
Actually the code I posted should convert any given string to "seo url" (ie. only alphanumeric characters and dashes). It works okay for me, but in any case, this is only one way of many how to do it (if you have the data in SQL database, you can do it directly in database too).
 
I'll test the code tomorrow

moreover guys, which one to you think is better among

123456-cheap-car-parts.html
123456-Cheap-Car-Parts.html
123456-cheapcarparts.html
123456-CheapCarParts.html

others?
 
123456-cheap-car-parts.html is the best one IMHO. And is the .html on the end needed?
 
no, I just thought maybe .html is loved more?
it's just for optimization

do you think 123456-cheap-car-parts without html is better?
 
thanks stanley

Houdas, the code is perfect, +rep
(if you have the data in SQL database, you can do it directly in database too).

what do you mean? how can I do it?
it might actually be better for the searches run against the db
 
Status
Not open for further replies.