Modify a PHP function to remove dash at beginning and ending

Hi experts,
I once got this function and it is good enough for sanitizing a name entry (most) to be used on URI.

There's only 1 thing that bothers me, that it cannot remove beginning & ending dash (-).
And yes I like this dash replacement feature, of course, as  long as they are not to be placed on beginning & ending :)

See a sample case to makes it clearer:

$test = '":heLLo WORLd:"';
echo sanitize($test);
// Output is  -hello-world-
// Output I'm looking for should be just hello-world

Below is the function codes ... looking forward for your help ... Thanks.
function sanitize( $string = '', $is_filename = FALSE, $force_lowercase = TRUE ) 
{ 
	// $is_filename = TRUE | ALLOWED the use of dot(.) AND underscore(_)

	// Replace all weird characters with dashes (-)
	$string = preg_replace('/[^\w_\.\-]+/u', '-', $string);

	if ( !$is_filename ) : $string = str_replace( array('_','.'), '-', $string ); 
	else : $string = preg_replace( '/\.+/u', '.', $string );
		   $string = preg_replace( '/\_+/u', '_', $string );
	endif;

	// Only allow one dash separator at a time (and make string lowercase)
	$string = preg_replace('/--+/u', '-', $string);
	//return mb_strtolower($string, 'UTF-8'); 
	return ( $force_lowercase ) ? ( (function_exists('mb_strtolower'))?mb_strtolower($string, 'UTF-8'):strtolower($string) ) : $string; 
}

Open in new window

LVL 1
trrsrrAsked:
Who is Participating?
 
Ray PaseurCommented:
So maybe after line 14, you would use something like this:

$string = preg_replace('/--+/u', '-', $string);
$string = trim($string, '-');

See http://php.net/manual/en/function.trim.php

HTH, ~Ray
0
 
Ray PaseurCommented:
You can use trim() to remove selected characters from the ends of a string.
0
 
Ovid BurkeConsultant InstructorCommented:
Try this:
function sanitize( $string = '', $is_filename = FALSE, $force_lowercase = TRUE ) 
{ 
	// $is_filename = TRUE | ALLOWED the use of dot(.) AND underscore(_)

	// Replace all weird characters with dashes (-)
	$string = preg_replace('/[^\w_\.\-]+/u', '-', $string);

	if ( !$is_filename ) : $string = str_replace( array('_','.'), '-', $string ); 
	else : $string = preg_replace( '/\.+/u', '.', $string );
		   $string = preg_replace( '/\_+/u', '_', $string );
	endif;

	// Only allow one dash separator at a time (and make string lowercase)
	$string = preg_replace('/--+/u', '-', $string);
	
	// remove das from left and right sides
	$string = ltrim($string, "-");
	$string = rtrim($string, "-");
	//return mb_strtolower($string, 'UTF-8'); 
	return ( $force_lowercase ) ? ( (function_exists('mb_strtolower'))?mb_strtolower($string, 'UTF-8'):strtolower($string) ) : $string; 
}

Open in new window

0
 
trrsrrAuthor Commented:
Excellent you guys ...

@Ray_Paseur:
I really thought that trim() works to remove whitespaces (only) ..  my bad :(

@madaboutasp:
I just noticed by now that there also rtrim() and ltrim() instead of trim() ..  my bad :(

And thanks for teaching me something new today ... @_@
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.