开发者

Best way to convert title into url compatible mode in PHP?

http://domain.name/1-As Low As 10% Downpayment, Free Golf Membership!!!

The abov开发者_运维知识库e url will report 400 bad request,

how to convert such title to user friendly good request?


You may want to use a "slug" instead. Rather than using the verbatim title as the URL, you strtolower() and replace all non-alphanumeric characters with hyphens, then remove duplicate hyphens. If you feel like extra credit, you can strip out stopwords, too.

So "1-As Low As 10% Downpayment, Free Golf Membership!!!" becomes:

as-low-as-10-downpayment-free-gold-membership

Something like this:

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;
}

You could probably shorten it with longer regexps but it's pretty straightforward as-is. The bonus is that you can use the same function to validate the query parameter before you run a query on the database to match the title, so someone can't stick silly things into your database.


See the first answer here URL Friendly Username in 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


You can use urlencode or rawurlencode... for example Wikipedia do that. See this link: http://en.wikipedia.org/wiki/Ichigo_100%25

that's the php encoding for % = %25


I just create a gist with a useful slug function:

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

You can use it to convert title to seo friendly url.

<?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


You could use the rawurlencode() function


To simplify just full the list of the variable $change_to and $to_change

<?php

// Just full the array list to make replacement complete
// In this space will change to _, à to just a



$to_change = [
    ' ', 'à', 'à', 'â','é', 'è', 'ê', 'ç', 'ù', 'ô', 'ö' // and so on
];

$change_to = [
    '_', 'a', 'a', 'a', 'e', 'e', 'e','c', 'u', 'o', 'o' // and so on
];

$texts = 'This is my slug in êlàb élaboré par';

$page_id = str_replace($to_change, $change_to, $texts);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜