Wednesday, May 11, 2016

PHP Tutorial 3

1: WORD CENSORING
<?php
$find = array('alex', 'billy', 'dale');
$replace=array('a**x', 'b***y', 'd**e');

if(isset($_POST['userinput']) && !empty($_POST['userinput'])){
$user_input=$_POST['userinput'];
// $user_input_lc = strtolower($user_input);
$user_input_new = str_ireplace($find, $replace, $user_input);
echo $user_input_new;
}

?>
<hr>
<form action="some2.php" method="POST">
<textarea name="userinput" rows="6" cols="30"><?php echo $user_input; ?></textarea><br><br>
<input type="submit" value="Submit">
</form>

2: Creating a Find and Replace Application
<?php
// Creating a Find and Replace Application

$offset = 0;

if(isset($_POST['text']) && isset($_POST['searchfor']) && isset($_POST['replacewith'])){

$text = $_POST['text'];
$search = $_POST['searchfor'];
$replace = $_POST['replacewith'];
$searchlength = strlen($search);

if(!empty($text) && !empty($search) && !empty($replace)){

while($strpos = strpos($text, $search, $offset)){
// echo $strpos.'<br>';
// echo $offset=$strpos+$searchlength.'<br>';
$offset = $strpos + $searchlength;
$text = substr_replace($text, $replace, $strpos, $searchlength);
}
echo $text;
} else {
echo 'Please fill in all fields';
}
}

?>
<form action="some2.php" method="POST">
<textarea  name="text" rows = "6" cols="30"></textarea><br><br>
Search for:<br>
<input type="text" name="searchfor"><br><br>
Replace with:<br>
<input type="text" name="replacewith"><br><br>
<input type="submit" value="Find and Replace">

</form>

3. RANDOM NUMBER
<?php
$rand = rand();
$max = getrandmax();
echo 'Some Random: '.$rand.'/'.$max;

if(isset($_POST['roll'])){
$rand = rand(1, 10);
echo '<br>You rolled a '.$rand;
}
?>
<form action = "some2.php" method="POST">
<input type="submit" name="roll" value="Roll dice.">

</form>

No comments:

Post a Comment