12
votes

Meilleure façon de convertir le titre en mode compatible URL en PHP?

http://domain.name/1-As Low As 10% Downpayment, Free Golf Membership!!!
The above url will report 400 bad request,how to convert such title to user friendly good request?

0 commentaires

6 Réponses :



9
votes

Voir la première réponse ici Nom d'utilisateur convivial URL à PHP? :

function Slug($string)
{
    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')), '-'));
}

$user = 'Alix Axel';
echo Slug($user); // alix-axel

$user = 'Álix Ãxel';
echo Slug($user); // alix-axel

$user = 'Álix----_Ãxel!?!?';
echo Slug($user); // alix-axel


1 commentaires

@ user198729: Pouvez-vous donner un exemple de sortie d'entrée / attente?



1
votes

Vous pouvez utiliser UrlenCode ou RawurlenCode ... Par exemple, Wikipedia fait cela. Voir ce lien: http://fr.wikipedia.org/wiki/ichigo_100%25

C'est le codage PHP pour% =% 25


0 commentaires


1
votes

Je viens de créer un gist avec une fonction limite utile:

https: //gist.github. com / ninjagab / 11244087 p>

Vous pouvez l'utiliser pour convertir le titre en URL convivial sur SEO. P>

<?php
class SanitizeUrl {

    public static function slug($string, $space="-") {
        $string = utf8_encode($string);
        if (function_exists('iconv')) {
            $string = iconv('UTF-8', 'ASCII//TRANSLIT', $string);
        }

        $string = preg_replace("/[^a-zA-Z0-9 \-]/", "", $string);
        $string = trim(preg_replace("/\\s+/", " ", $string));
        $string = strtolower($string);
        $string = str_replace(" ", $space, $string);

        return $string;
    }
}

$title = 'Thi is a test string with some "strange" chars ò à ù...';
echo SanitizeUrl::slug($title);
//this will output:
//thi-is-a-test-string-with-some-strange-chars-o-a-u


1 commentaires

De cette façon, vous aurez plusieurs "-" lorsque vous avez plusieurs espaces de votre chaîne.



-1
votes

Simplifier simplement la liste de la liste de la variable $ Change_to et $ to_change xxx


0 commentaires