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?
6 Réponses :
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
@ user198729: Pouvez-vous donner un exemple de sortie d'entrée / attente?
Vous pouvez utiliser UrlenCode ou RawurlenCode ... Par exemple, Wikipedia fait cela. Voir ce lien: http://fr.wikipedia.org/wiki/ichigo_100%25 P >
C'est le codage PHP pour% =% 25 P>
Vous voudrez peut-être utiliser une "limace" à la place. Plutôt que d'utiliser le titre Verbatim comme URL, vous Donc "1-Aussi bas que 10% d'acompte, membre de golf libre !!!" Devient: p> Quelque chose comme ceci: P> Strtolower () Code>
et remplacez tous les caractères non alphanumériques avec des traits d'union, puis supprimez des traits d'union en double. Si vous vous sentez comme un crédit supplémentaire, vous pouvez supprimer Stopwords A >, aussi. AS-Low-AS-10-ADVAYMENT-GOLD-AXITION CODE> P>
function sluggify($url)
{
# Prep string with some basic normalization
$url = strtolower($url);
$url = strip_tags($url);
$url = stripslashes($url);
$url = html_entity_decode($url);
# Remove quotes (can't, etc.)
$url = str_replace('\'', '', $url);
# Replace non-alpha numeric with hyphens
$match = '/[^a-z0-9]+/';
$replace = '-';
$url = preg_replace($match, $replace, $url);
$url = trim($url, '-');
return $url;
}
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
De cette façon, vous aurez plusieurs "-" lorsque vous avez plusieurs espaces de votre chaîne.
Simplifier simplement la liste de la liste de la variable $ Change_to code> et
$ to_change code>