homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

How to replace space " " with underscore " "
php, replace

 10:49 pm on Jan 11, 2014 (gmt 0)

I need a little help. I have created the below script. A form that save its info to my database and a long with that it create some (2 on different paths) folders on the server.

My issue is that in the form under ”folder” you can type the name of the folder you want to create along with the info that is saved in the database. At the moment everything works just as it should. My request/issue is how do a replace space “ “ from the folder name to an underscore “_”

Means, if someone enters a folder name called “My Folder Photo” I need the script to change the name to: “my_folder_photo” so it will work in urls on the website.

The is my codes so fare:
include "../config.php";
mysql_query("insert into profiles set name= '".$_POST["name"]."', age= '".$_POST["age"]."', country = '".$_POST["country"]."', folder = '".$_POST["folder"]."'");

$folder = $_POST['folder'];
$path1 = '../game1/photos/' . $folder;
$path2 = '../game2/photos/' . $folder;
header("Location: myprofile-new-step2.php");



<form method="post" action="<?=$_SERVER["PHP_SELF"]?>" name="myform" id="myform">
<table bgcolor="#FFFFFF" border="0" align="center"><tr><td width="70">
</td><td width="270">
<input type="text" name="name" size="40">

<input type="text" name="age" size="40">

<select name="country" data-placeholder="<?=$country?>" class="chosen-select" style="width:250px;" id="<?=$country?>" value="<?=$country?>" <?= (!empty($country) ? 'checked="checked"' : "")?>>
<option value="<?=$country?>"></option>
<option value="United States">United States</option>
<option value="United Kingdom">United Kingdom</option>
Enter the country you live in.
<input type="text" name="folder" size="40" id="folder">

<center><br />
<input type="submit" name="send" title="NEXT" value="NEXT"></center>

Im a new to php so need as much help as possible to figure this out :) THANKS



 1:35 am on Jan 12, 2014 (gmt 0)

:: detour to php dot net to double-check ::

Won't the basic str_replace [us3.php.net] do it?

Or are you asking exactly where in the code the replacement needs to happen?


 2:19 am on Jan 31, 2014 (gmt 0)

This is probably too late to help, but:

$folder = strtolower(str_replace(" ", "_", $_POST['folder']));

You should probably replace all non-alphanumeric character, though, because you're going to have a problem accessing a folder with a tab, carriage return, or special character in the name. So, maybe:

$folder = strtolower(preg_replace("/[^A-Za-z0-9_]/g", "_", $_POST['folder']));

I just typed that up for this, so I haven't tested it for errors. Theoretically, it should replace anything other than a letter, number, or underscore with an underscore.


 8:11 am on Jan 31, 2014 (gmt 0)

GoNC, Its perfect and working thanks :)


 8:45 am on Jan 31, 2014 (gmt 0)

Do you mind if I suggest a few other changes to your script?

if (isset($_POST['send'])) {
$result = false;

// Use sprintf and mysql_real_escape_string to make sure no one
// submits bad data; especially those trying to break in to your
// database
$query = sprintf("INSERT INTO profiles (name, age, country, folder) VALUES ('%s', '%s', '%s', '%s')",

// If the query is successful, $result will be changed to true
$result = mysql_query($query);

// So now, you can test to make sure the insert worked
if (!$result) {
// insert failed, give an error or something

else {
$folder = strtolower(preg_replace("/[^A-Za-z0-9_]/g", "_", $_POST['folder']));

$path1 = '../game1/photos/' . $folder;
$path2 = '../game2/photos/' . $folder;


header("Location: myprofile-new-step2.php");

Again, that's not tested, so beware any typos. I hope this helps!

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved