PHP generate permalink through php and javascript

This Article explains how to generate permalink through php and javascript code.

 

Using Javascript:

<script type="text/javascript">
    function string_to_permalink(str) {
        str = str.replace(/^\s+|\s+$/g, ''); // trim
        str = str.toLowerCase();
        // remove accents, swap ñ for n, etc
        var from = "àáäâèéëêìíïîòóöôùúüûñç·/_,:;";
        var to   = "aaaaeeeeiiiioooouuuunc------";
        for (var i=0, l=from.length ; i<l ; i++) {
            str = str.replace(new RegExp(from.charAt(i), 'g'), to.charAt(i));
        }
        str = str.replace(/[^a-z0-9 -]/g, '') // remove invalid chars
          .replace(/\s+/g, '-') // collapse whitespace and replace by -
          .replace(/-+/g, '-'); // collapse dashes
        return str;
    }
</script>

 

Using PHP :

function generate_permalink($string)
{
	if($string !== mb_convert_encoding( mb_convert_encoding($string, 'UTF-32', 'UTF-8'), 'UTF-8', 'UTF-32') )
		$string = mb_convert_encoding($string, 'UTF-8', mb_detect_encoding($string));
	$string = htmlentities($string, ENT_NOQUOTES, 'UTF-8');
	$string = preg_replace('`&([a-z]{1,2})(acute|uml|circ|grave|ring|cedil|slash|tilde|caron|lig);`i', '\\1', $string);
	$string = html_entity_decode($string, ENT_NOQUOTES, 'UTF-8');
	$string = preg_replace(array('`[^a-z0-9]`i','`[-]+`'), '-', $string);
	$string = strtolower( trim($string, '-') );
	return $string;
}

 

 

One thought on “PHP generate permalink through php and javascript

Leave a Reply

Your email address will not be published. Required fields are marked *