Jump to content

Format String, Replace Whitespaces With Slashes, Remove Forward/Backward Slashes


JustinK101

Recommended Posts

I need a way to take a string of text:

 

example: "Computers / Hardware"

 

And make it lowercase, remove any spaces (replace with _), and strip any forward or backward slashes. So the example becomes:

 

example fixed: "computers_hardware"

 

Thanks for the help.

Premiso,

 

Thanks for the reply. Worked except on the case:

 

example: "Xbox Games"

 

output: "xboxgames"

 

Is it possible to put an underscore between xbox and games, so its "xbox_games"

<?php
$string = "Xbox Games";
$string = strtolower($string);
$string = preg_replace("/\s+/", "_", $string);
$string = str_replace(array('\\', '/'), '', $string);
$string = preg_replace("/_+/", "_", $string);
echo $string;
?>

 

Might want to put that into a function.

Darkwater,

 

Perfect, you are the man. Thanks greatly. Below is the final function for others to use if they need:

 

function url_text($text) {
		$text = strtolower($text);
		$text = preg_replace("/\s+/", "_", $text);
		$text = str_replace(array('\\', '/'), '', $text);
		$text = preg_replace("/_+/", "_", $text);
		return($text);
	}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.