开发者

PHP - a function to "sanitize" a string

is there any PHP func开发者_运维技巧tion available that replaces spaces and underscores from a string with dashes?

Like:

Some Word

Some_Word

Some___Word

Some     Word

Some ) # $ ^ Word

=> some-word

basically, the sanitized string should only contain a-z characters, numbers (0-9), and dashes (-).


This should produce the desired result:

$someword = strtolower(preg_replace("/[^a-z]+/i", "-", $theword));


<?php
function sanitize($s) {
    // This RegEx removes any group of non-alphanumeric or dash
    // character and replaces it/them with a dash
    return strtolower(preg_replace('/[^a-z0-9-]+/i', '-', $s));
}

echo sanitize('Some Word') . "\n";
echo sanitize('Some_Word') . "\n";
echo sanitize('Some___Word') . "\n";
echo sanitize('Some     Word') . "\n";
echo sanitize('Some ) # $ ^ Word') . "\n";

Output:

Some-Word
Some-Word
Some-Word
Some-Word
Some-Word


You might like to try preg_replace: http://php.net/manual/en/function.preg-replace.php

Example from this page:

<?php
$string = 'April 15, 2003';
$pattern = '/(\w+) (\d+), (\d+)/i';
$replacement = '${1}1,$3';
echo preg_replace($pattern, $replacement, $string);
//April1,2003
?>

You might like to try a search for "search friendly URLs with PHP" as there is quite a bit of documentation, example:

function friendlyURL($string){
$string = preg_replace("`\[.*\]`U","",$string);
$string = preg_replace('`&(amp;)?#?[a-z0-9]+;`i','-',$string);
$string = htmlentities($string, ENT_COMPAT, 'utf-8');
$string = preg_replace( "`&([a-z])(acute|uml|circ|grave|ring|cedil|slash|tilde|caron|lig|quot|rsquo);`i","\\1", $string );
$string = preg_replace( array("`[^a-z0-9]`i","`[-]+`") , "-", $string);
return strtolower(trim($string, '-'));
}

and usage:

$myFriendlyURL = friendlyURL("Barca rejects FIFA statement on Olympics row");
echo $myFriendlyURL; // will echo barca-rejects-fifa-statement-on-olympics-row

Source: http://htmlblog.net/seo-friendly-url-in-php/


I found a few interesting solutions throughout the web.. note none of this is my code. Simply copied here in hopes of helping you build a custom function for your own app.

This has been copied from Chyrp. Should work well for your needs!

/**
 * Function: sanitize
 * Returns a sanitized string, typically for URLs.
 *
 * Parameters:
 *     $string - The string to sanitize.
 *     $force_lowercase - Force the string to lowercase?
 *     $anal - If set to *true*, will remove all non-alphanumeric characters.
 */
function sanitize($string, $force_lowercase = true, $anal = false) {
$strip = array("~", "`", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "_", "=", "+", "[", "{", "]",
               "}", "\\", "|", ";", ":", "\"", "'", "&#8216;", "&#8217;", "&#8220;", "&#8221;", "&#8211;", "&#8212;",
               "—", "–", ",", "<", ".", ">", "/", "?");
$clean = trim(str_replace($strip, "", strip_tags($string)));
$clean = preg_replace('/\s+/', "-", $clean);
$clean = ($anal) ? preg_replace("/[^a-zA-Z0-9]/", "", $clean) : $clean ;
return ($force_lowercase) ?
    (function_exists('mb_strtolower')) ?
        mb_strtolower($clean, 'UTF-8') :
        strtolower($clean) :
    $clean;
}

EDIT: Even easier function I found! Just a few lines of code, fairly self-explanitory.

function slug($z){
    $z = strtolower($z);
    $z = preg_replace('/[^a-z0-9 -]+/', '', $z);
    $z = str_replace(' ', '-', $z);
    return trim($z, '-');
}


Not sure why @Dagon chose to leave a comment instead of an answer, but here's an expansion of his answer.

php's preg_replace function allows you to replace anything with anything else.

Here's an example for your case:

$input = "a word 435 (*^(*& HaHa";

$dashesOnly = preg_replace("#[^-a-zA-Z0-9]+#", "-", $input);

print $dashesOnly; // prints a-word-435-HaHa;


You can think of writing this piece of code with the help of regular expressions.

But I dont see any available functions which help you directly replace the " " with "-"

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜