SEO Friendly links, js and/or php stripping

别来无恙 提交于 2020-01-14 04:14:23

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!