开发者

any special php function to make a string 'url' friendly for address-bar?

Is there any such function?

If not, anybody got one?

I need to make a strings submitted by users 'url friendly' because I will later on开发者_Go百科 use it as an url to a post on my site.

Thanks

BTW, its PHP!


It sounds like you want to take some bit of user input

Enter Name: ___Bob Smith_____

And then later on use that input as part of a URL

http://example.com/bob-smith

If that's what you're after, there's no PHP function that will magically do it for you. My approach on something like this is to

  1. Sanitize the name down so it's URL safe

  2. If necessary, add a unique database identifier to the end of the string

Number 1 is pretty easy with a regex

$url = strToLower(preg_replace('%[^a-z0-9_-]%six','-',$name)); //does a-z catch unicode?

That will turn Bob Smith into bob-smith.

If getting a unique fragment for each string is important to you, you'll need to come up with some schemes for #2. Consider the following strings

Bob  Smith
Bob" Smith

They'll both be sanitized down to

Bob--Smith

Chances are you're storing this information in a database, so appending the primary key to the string will work. You could also incorporate the primary key as part of the URL. For example, assuming the primary key is a simple integer auto_increment.

http://example.com/27/bob-smith/
http://example.com/bob-smith_27


urlencode()


There's urlencode, but I'm not sure if that's exactly what you're looking for


Provided you are running Apache, I think you are looking for mod_rewrite, an Apache module that allows to re-write user-friendly URLs. A guide for beginners can be found here. Before you delve into it, make sure your host or server supports mod_rewrite.


If you want to have non ASCII characters to be shows as they are and not the percent-encoded words, you need to use Unicode and encode the URL with UTF-8. So if you want to use the Ä character in your URL, use the UTF-8 encoded word 0xC384 instead of, for example, the ISO 8859-1 encoded word 0xC4. The difference (take a look at the URL in your status bar):

Ä
Ä


May be what you want is something like a wordpress post slug. In wordpress, the function (sanitize_title_with_dashes) basically sanitizes input, encodes and creates a '-' seperated url.


I do believe that what you want is something to turn New Storm Hits Winter! into something like new-storm-hits-winter.

This function is "stolen" from CodeIgniter and does exactly that:

/**
 * Create URL Title
 *
 * Takes a "title" string as input and creates a
 * human-friendly URL string with either a dash
 * or an underscore as the word separator.
 *
 * @access  public
 * @param   string  the string
 * @param   string  the separator: dash, or underscore
 * @return  string
 */
if ( ! function_exists('url_title'))
{
    function url_title($str, $separator = 'dash', $lowercase = FALSE)
    {
        if ($separator == 'dash')
        {
            $search     = '_';
            $replace    = '-';
        }
        else
        {
            $search     = '-';
            $replace    = '_';
        }

        $trans = array(
                        '&\#\d+?;'              => '',
                        '&\S+?;'                => '',
                        '\s+'                   => $replace,
                        '[^a-z0-9\-\._]'        => '',
                        $replace.'+'            => $replace,
                        $replace.'$'            => $replace,
                        '^'.$replace            => $replace,
                        '\.+$'                  => ''
                      );

        $str = strip_tags($str);

        foreach ($trans as $key => $val)
        {
            $str = preg_replace("#".$key."#i", $val, $str);
        }

        if ($lowercase === TRUE)
        {
            $str = strtolower($str);
        }

        return trim(stripslashes($str));
    }
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜