0

I have a site where people post stories, and I want part of each stories title to be included in the url that points to the story.

For example if a poster posts a story titled "this is an example post", I am looking for a way to translate it to "this-is-an-example-post".

Also, because the title will be used in a URL I need to filter out all the reserved or potentially dangerous characters.

Is there an already made way (function library whatever) to do that or I will have to write my own?

ppp
  • 2,035
  • 9
  • 32
  • 52
  • 3
    possible duplicate of [Sanitizing strings to make them URL and filename safe?](http://stackoverflow.com/questions/2668854/sanitizing-strings-to-make-them-url-and-filename-safe) – j08691 Jun 25 '12 at 17:11
  • First replace all accented letters by their non accented equivalents. Next replace all spaces by underscores. Finally remove everything except alpha-numeric characters and underscores. Done. – Anne Jun 25 '12 at 17:14
  • 1
    Out of curiosity, won't PHP's `urlencode()` solve your problem? http://php.net/manual/en/function.urlencode.php – ametren Jun 25 '12 at 17:19

3 Answers3

0

Little php function to make frindly urls.

public function friendly_url($string) {
        $table = array(
            'Š' => 'S',         'š' => 's',
            'Ŀ' => 'Dj',
            'đ' => 'dj',
            'Ž' => 'Z',
            'ž' => 'z',
            'Č' => 'C',
            'Ŀ' => 'c',
            'Ć' => 'C',
            'ć' => 'c',
            'À' => 'A',
            'ÿ' => 'A',
            'Â' => 'A',
            'Ã' => 'A',
            'Ä' => 'A',
            'Å' => 'A',
            'Æ' => 'A',
            'Ç' => 'C',
            'È' => 'E',
            'É' => 'E',
            'Ê' => 'E',
            'Ë' => 'E',
            'Ì' => 'I',
            'ÿ' => 'I',
            'Î' => 'I',
            'ÿ' => 'I',
            'Ñ' => 'N',
            'Ò' => 'O',
            'Ó' => 'O',
            'Ô' => 'O',
            'Õ' => 'O',
            'Ö' => 'O',
            'Ø' => 'O',
            'Ù' => 'U',
            'Ú' => 'U',
            'Û' => 'U',
            'Ü' => 'U',
            'ÿ' => 'Y',
            'Þ' => 'B',
            'ß' => 'Ss',
            'à' => 'a',
            'á' => 'a',
            'â' => 'a',
            'ã' => 'a',
            'ä' => 'a',
            'å' => 'a',
            'æ' => 'a',
            'ç' => 'c',
            'è' => 'e',
            'é' => 'e',
            'ê' => 'e',
            'ë' => 'e',
            'ì' => 'i',
            'í' => 'i',
            'î' => 'i',
            'ï' => 'i',
            'ð' => 'o',
            'ñ' => 'n',
            'ò' => 'o',
            'ó' => 'o',
            'ô' => 'o',
            'õ' => 'o',
            'ö' => 'o',
            'ø' => 'o',
            'ù' => 'u',
            'ú' => 'u',
            'û' => 'u',
            'ý' => 'y',
            'ý' => 'y',
            'þ' => 'b',
            'ÿ' => 'y',
            'Ŕ' => 'R',
            'ŕ' => 'r',
            ' ' => '-'
        );
        $string = strtr($string, $table);
        $string = strtolower($string);
        $string = preg_replace("/[^a-zA-Z0-9_.-]/", "", $string);

        return $string;
    }
Oswaldo Acauan
  • 2,730
  • 15
  • 23
0

Why not try something like:

$title = "this is an example post";
$url = str_replace(' ', '-', $title);

and then take a look at the urlencode section of the PHP manual for many examples in the comments.

Jay
  • 18,959
  • 11
  • 53
  • 72
0

I just found this question, on a similar topic.

The function given in the first answer was enough to translate all non url-friendly characters to '-'.

Then I used an array like Oswaldo suggests in his answer and strtr to translate the characters I wanted (greek) to english.

The end product was this:

function titleToUrlFriendlyTitle($string){
        $table = array('Α'=>'a','Ά'=>'a','ά'=>'a', 'α'=>'a', 'Β'=>'b', 'β'=>'b', 'γ'=>'g', 'Γ'=>'g', 'Δ'=>'d', 'δ'=>'d', 'έ'=>'e', 'ε'=>'e', 'Ε'=>'e', 'Έ'=>'e', 'ζ'=>'z', 'Ζ'=>'z', 'η'=>'i', 'Η'=>'i', 'ή'=>'i', 'Ή'=>'i', 'θ'=>'th', 'Θ'=>'th', 'ι'=>'i', 'Ι'=>'i', 'ί'=>'i', 'Ί'=>'i', 'ϊ'=>'i', 'Ϊ'=>'i', 'ΐ'=>'i', 'κ'=>'k', 'Κ'=>'k', 'λ'=>'l', 'Λ'=>'l', 'μ'=>'m', 'Μ'=>'m', 'ν'=>'n', 'Ν'=>'n', 'ξ'=>'ks', 'Ξ'=>'ks', 'ο'=>'o', 'Ο'=>'o', 'Ό'=>'o', 'ό'=>'o', 'π'=>'p', 'Π'=>'p', 'ρ'=>'r', 'Ρ'=>'r', 'σ'=>'s', 'Σ'=>'s', 'τ'=>'t', 'Τ'=>'t', 'Υ'=>'u', 'υ'=>'u', 'Ύ'=>'u', 'ύ'=>'u', 'ϋ'=>'u', 'Ϋ'=>'u', 'ΰ'=>'u', 'φ'=>'f', 'Φ'=>'f', 'χ'=>'x', 'Χ'=>'x', 'Ψ'=>'ps', 'ψ'=>'ps', 'ω'=>'o', 'Ω'=>'o', 'ώ'=>'o', 'Ώ'=>'o');
        $string = strtr( $string , $table );
        return strtolower(trim(preg_replace('~[^0-9a-z]+~i', '-', html_entity_decode(preg_replace('~&([a-z]{1,2})(?:acute|cedil|circ|grave|lig|orn|ring|slash|th|tilde|uml);~i', '$1', htmlentities($string, ENT_QUOTES, 'UTF-8')), ENT_QUOTES, 'UTF-8')), '-'));
        }
Community
  • 1
  • 1
ppp
  • 2,035
  • 9
  • 32
  • 52