问题
I have seen this being done on the wordpress and i dont have access to word press :)
but i need to return a url string removing any non valid characters from it and converting some characters into appropriate characters :)
e.g.
1+ characters should be converted (of the following)
[space] = [dash] (1 dash) >>> (-)
[underscore] = [dash] (1 dash) >>> (-)
$str = 'Hello WORLD this is a bad string';
$str = convert_str_to_url($str);
//output//NOTE: caps have are lowercase :)
//hello-world-bad-string
and remove common and senseless words such as "the","a","in" etccc
at least point me on the right direction if u dnt have a gd code :)
回答1:
What you want is the "slugged" string. Here's a list of relevant links:
- best way to escape and create a slug
- Convert any title to url slug and back from url slug to title
- how to generate slugs in php
- http://snipplr.com/view/2809/convert-string-to-slug/
Just google PHP slug
for more examples.
回答2:
strtr
can be used for this:
$replace = array(
' ' => '-',
'_' => '-',
'the' => '',
...
);
$string = strtr($string, $replace);
回答3:
I would create a function with the str_replace() function. For example:
$str = 'Sentence with some words';
$str = strtolower($str);
$searchNone = array('the', 'a', 'in');
$replaceNone = '';
$str = str_replace($searchNone, $replaceNone, $str);
$search = array(chr(32)); //use ascii
$replace = '-';
$str = str_replace($search, $replace, $str);
echo $str;
Use the following site for the special chars: http://www.asciitable.com/.
回答4:
Maybe something like:
function PrettyUri($theUri)
{
$aToBeReplace = array(' then ', ' the ', ' an '
, ' a ', ' is ', ' are ', ' ', '_');
$aReplacements = array(' ', ' ', ' '
, ' ', ' ', ' ', '-', '-');
return str_replace($aToBeReplace, $aReplacements, strtolower($theUri));
}
echo PrettyUri('Hello WORLD this is a bad string');
来源:https://stackoverflow.com/questions/4758881/seo-friendly-links-js-and-or-php-stripping