开发者

Turn String With Spaces and Characters Into URL-ready Address (like Wordpress, etc) Using PHP

I've built a custom CMS that does the usual things: post management, content management, contact management, etc.

In the post management section, I would like to extract the "Title" field and convert this into a URL-ready form.

Example: New 开发者_如何学Gopost is created titled "3 Ways to Win in Real Estate & in Life". I want this to run through a PHP script that turns it into "3_ways_to_win_in_real_estate_&_in_life".

Anyone have a script for this, or would url_encode() do all of this for me?


Make use of currently developed code that you can use within your own projects.

Kohana 3 framework has solution for you. Below you can find solution on the basis of URL::title() method from Kohana 3 framework:

function title($title, $separator = '-') {
    // Remove all characters that are not the separator, letters, numbers, or whitespace
    $title = preg_replace('![^' . preg_quote($separator) . '\pL\pN\s]+!u', '', strtolower($title));

    // Replace all separator characters and whitespace by a single separator
    $title = preg_replace('![' . preg_quote($separator) . '\s]+!u', $separator, $title);

    // Trim separators from the beginning and end
    return trim($title, $separator);
}


function cleanURL($string)
{
    $url = str_replace("'", '', $string);
    $url = str_replace('%20', ' ', $url);
    $url = preg_replace('~[^\\pL0-9_]+~u', '-', $url); // substitutes anything but letters, numbers and '_' with separator
    $url = trim($url, "-");
    $url = iconv("utf-8", "us-ascii//TRANSLIT", $url);  // you may opt for your own custom character map for encoding.
    $url = strtolower($url);
    $url = preg_replace('~[^-a-z0-9_]+~', '', $url); // keep only letters, numbers, '_' and separator
    return $url;
}

// echo cleanURL("Shelly's%20Greatest%20Poem%20(2008)");  // shellys-greatest-poem-2008

from here. You can write your own or possibly find one to replace things like & with and, and so on.

Also note that this function uses dashes, not underscores. The preferred way to create clean URLs is with dashes, not underscores.


This is basic, but works.

  static public function slugify($text)
  {
    // replace all non letters or digits by -
    $text = preg_replace('/\W+/', '-', $text);

    // trim and lowercase
    $text = strtolower(trim($text, '-'));

    return $text;
  }

From here: http://www.symfony-project.org/jobeet/1_4/Doctrine/en/05


"Anyone have a script for this, or would url_encode() do all of this for me?" Have you tried using url_encode() to do this for you? A quick test script would have revealed that much for you, or even using functions-online.com's urlencode() tester.

$str = '3 Ways to Win in Real Estate & in Life';
echo urlencode( $str );
// 3+Ways+to+Win+in+Real+Estate+%26+in+Life

You could use a simple preg_replace() and simple replace anything which is not a letter or digit with either an underline or a dash.

echo preg_replace( '/[^\d\w]+/' , '_' , $str );
// 3_Ways_to_Win_in_Real_Estate_in_Life
echo preg_replace( '/[^\d\w]+/' , '-' , $str );
// 3-Ways-to-Win-in-Real-Estate-in-Life


Just use a dash of str_replace to turn the spaces into underscores, and a sprinkle of urlencode to catch the rest.

Edit: I missed the strtolower part, but I think you had a handle on that.

This is of course just a basic way to go about it, if you want to exactly imitate the wordpress way of turning a text into a URL, have a look at that code, it's open and available for you to do so.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜