[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 314
  • Last Modified:

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

0
trrsrr
Asked:
trrsrr
  • 2
2 Solutions
 
Ray PaseurCommented:
You can use trim() to remove selected characters from the ends of a string.
0
 
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
 
Ovid BurkeCreative DirectorCommented:
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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now