简体   繁体   中英

SEO Friendly links, js and/or php stripping

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 :)

eg

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 :)

strtr can be used for this:

$replace = array(
   ' ' => '-',
   '_' => '-',
   'the' => '',
   ...
);

$string = strtr($string, $replace);

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/ .

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');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM