Jump to content

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


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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.