1

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

Val
  • 17,336
  • 23
  • 95
  • 144

4 Answers4

4

What you want is the "slugged" string. Here's a list of relevant links:

Just google PHP slug for more examples.

Community
  • 1
  • 1
Damiqib
  • 1,067
  • 2
  • 13
  • 23
1

strtr can be used for this:

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

$string = strtr($string, $replace);
Per H
  • 1,542
  • 10
  • 19
0

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

Dennis Enderink
  • 346
  • 1
  • 5
0

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');
Richard Knop
  • 81,041
  • 149
  • 392
  • 552