PHP Tutorials- How To Replace Change Certain Letters Strings In Forms
php tutorials- how to replace change certain letters strings in forms
php, tutorials, how, to, replace, change, certain, letters, strings, in, forms
PHP Tutorials- How To Replace Change Certain Letters Strings In Forms
Post Description: php tutorials- how to replace change certain letters strings in forms
POST# 348
Posted On: Fri Feb 29, 2008 12:01 pm
leni
Topic: PHP Tutorials- How To Replace Change Certain Letters Strings In Forms
In this short tutorial guide, my goal is to share what i know about php.
i wrote this post to show whoever is visiting this forum and whats to know about php.

I had a situation where i had a web form, and my visitors where entering bad words as their names. for example, one field of the form as called, user_name, and some people were puting a bad word as their name, so to prevent people from using these words, PHP offered a neat and cool and useful function called: str_replace()

How does the str_replace() function work?

official this is the description:
str_replace = Replace all occurrences of the search string with the replacement string

Description
mixed str_replace ( mixed search, mixed replace, mixed subject [, int &count] )

basically what this functions does, it looks for a particular character, letter or number (strings) and replaces it to whatever i want to.

Real Life Example:

Suppose i have a form, and the form ask's the user's name, this is how the form would look like:

     Code:
<form method="post" action="">
<input type="text" name="user_name" value="bad_word">
<input type="submit" name="Submit" value="Submit">
</form>


In the form above, the value of user_name is "bad_word", in this case i want to change "bad_word" to "not_allowed", so when i submit the form, my php code would look like this


     Code:
<?php
# initial post value of of user_name is bad_word
$user_name=$_POST['name'];

# if user_name is equal to "bad_word", change it to "not_allowed"
if($user_name== "bad_word") {
$user_name = str_replace('bad_word', 'not_allowed',$user_name);
}

#now user_name has the value of "not_allowed"
echo $user_name;

?>


Remeber, i basically changed "bad_word" to "not_allowed", but you can change any string to whatever you what.


No Repies
(0) Comments for PHP Tutorials- How To Replace Change Certain Letters Strings In Forms

What do you think?

* name:  

* email:  

* Please enter comments:


Receive Replies on my Comments
(An email will be sent to you when someone replies to your comments)

Add image to comments
yes no             upload