Quantcast
Viewing latest article 3
Browse Latest Browse All 7

Answer by Jake for PHP - a function to "sanitize" a string

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, '-');}

Viewing latest article 3
Browse Latest Browse All 7

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>