PHP Tutorials- How to Replace Change Certain Letters Strings in Forms

Mobile
feeds
Welcome Login | Register

PHP Tutorials- How to Replace Change Certain Letters Strings in Forms
Reply Your Comments:
Click this button if you are interested in replying to this topic and leave your comments
Sent To Friend
CLick this button if you want to send this page to a friend.
Subsribe To Rss Feeds
Subscribe to RSS
CLick this button if you want to subscribe to this RSS Feed. You can use your browsers feeds burners if you have mozilla or internet explorer 7 or higher and keep up with updates.
  Forums Index
      » PHP Forums
        » » 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 PHP
Post Tags:
This Post Has Been Viewed 721 Times Since Wed Nov 29, 2006 10:42 am Author leni with 0 replies
PHP Tutorials- How to Replace Change Certain Letters Strings in Forms
Advertise On This Page




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.


Leave Your Comments

Your Name
Your Email Address (Will Not Be Published)
Notify Me When Someone Replies to this Page
(An email will be sent to you when someone replies to your comments)
Your Comments
Include A Picture with your comments
Share
| More
Share this page by putting this URL in your comments to other websites like myspace, Facebook, Twitter friendster, Hi5, Groups, Boards, Forum or others. Just Copy and Paste this Code
URL:
To embed this topic, just copy the code from the "Embed" box. Once you've copied the code, just paste it into your website or blog to embed it.
Embed:
BBCODE is use on forums. You can put this code on all your BBCODE enabled forums like PhpBB, vBulletin® and others. Just Copy and Paste this code on your Posts and Replies on your forums
BBCODE:
Subscribe Feeds
Webmasters - Exchange Links With Us. Add related websites to this topic
Add Link:
Links Related to : PHP Tutorials- How to Replace Change Certain Letters Strings in Forms

Comments and replies About PHP Tutorials- How to Replace Change Certain Letters Strings in Forms
(0) Comments