- Forums
- PHP
- PHP Tutorials- How To Replace Change Certain Letters Strings In Forms
This Page Contains information about PHP Tutorials- How To Replace Change Certain Letters Strings In Forms By leni in category PHP with 0 Replies. [976], Last Updated: Sat May 18, 2024
leni
Wed Nov 29, 2006
0 Comments
996 Visits
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.