I have the following string in a text file: - php

I have a code but is not working, I'm trying use explode but still not working.
My code have two strings:
^#^b = (on)
^A^b = (off)
I try to get only 2 characters from a .txt file in a variable like:
$on=^#
$off=^A
then I can use the result to verify the state of light using a dimmer.
Code
<?php
if(isset($_POST['estado'])) {
exec('/var/www/www.rfteste.com/htdocs/estado.sh');
}
if(isset($_POST['ligar'])) {
exec('/var/www/www.rfteste.com/htdocs/liga.sh');
}
if(isset($_POST['desligar'])) {
exec('/var/www/www.rfteste.com/htdocs/desliga.sh');
}
echo "<H3>CONTROL PANEL</H3>";
$str = file_get_contents("/var/www/www.rfteste.com/htdocs/estado.txt");
$vals = explode("^", $str);
$num1 = "^".$vals[0];
$num2 = "^".$vals[1];
$onoff= "^A";
if($num2 == $onoff)
echo "<b>on</b>";
else
echo "<b>off</b>";
?>
<html>
<body>
<form method="post" action="">
<p>
<center><input type="submit" value="Ligar" name="ligar""';" /></center>
<center><input type="submit" value="desligar" name="desligar""';" /></center>
<center><input type="submit" value="atualizar" name="estado""';" /></center>

i think this can help you solve your problem
function checktext($text){
if(preg_match("'#'", $text)){
return 'on';
}else{
return 'off';
}
}
echo checktext('^A^b');

Use php substr()
if(substr($str, 0, 1) == "A")
{
echo "<b>on</b>";
} else {
echo "<b>off</b>";
}

Related

Store post form values in hidden input array

I'm new to php and here's my code for a number guessing game. Since I don't know how to define a random number, I chose the number myself, 80. I'm trying to store all the guesses prior to the right one, and after the right guess, print them out on the screen. But I can't seem to be able to get it right since it only prints only the last guess before the right one.
Any help is appreciated!
<html>
<head>
</head>
<body>
<?php
$allguesses = array();
if($_SERVER["REQUEST_METHOD"] == "POST"){
$t = $_POST["guess"];
$sayi = 80;
if($sayi >$t){
echo 'Guess higher';
}elseif($sayi == $t){
echo "You've guessed it right!<br>";
echo 'Guessed numbers: <br>';
foreach($_POST["tmn"] as $y){
echo $y . ',';
}
}else{
echo 'Guess lower';
}
array_push($allguesses,$t);
}
?>
<form method="post">
Guess the number:
<input type="number" name="guess" min ="1" max = "100"><br>
<input type="submit" name="submit">
<?php
foreach($allguesses as $x){
echo "<input type ='hidden' name = 'tmn[]' value=' ".$x . " '>";
}
?>
</form>
</body>
</html>
Sessions seem the best for where you are in your learning curve.
The session allows the normal stateless http protocol to remember things between each submission of a form or forms. So we will save each guess in an array of guesses in the SESSION, which in PHP is an array as well.
<?php
session_start(); // create a session, or reconnect to an existing one
if( $_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST["guess"]) ) {
$_SESSION['guesses'][] = $_POST["guess"]; // keep an array of guesses
// $t = $_POST["guess"]; no need for extra variables on the stack
$sayi = 80;
if($sayi > $_POST["guess"]){
echo 'Guess higher';
}elseif($sayi == $_POST["guess"]){
echo "You've guessed it right!<br>";
echo 'Guessed numbers: <br>';
foreach($_SESSION['guesses'] as $guess){
echo $guess . ',';
}
$_SESSION['guesses'] = array(); // clear the old guesses out
}else{
echo 'Guess lower';
}
}
?>
<html>
<head>
</head>
<body>
<form method="post">
Guess the number:
<input type="number" name="guess" min ="1" max = "100"><br>
<input type="submit" name="submit">
</form>
</body>
</html>
Removed uneeded codes.
Changed method of showing previous guesses with the below code.
echo implode( ", ", $_POST["tmn"] ); // cleaner
This block handles storing previous guesses into an array that is used for displaying previous guesses.
if( isset( $_POST ) ) {
$_POST["tmn"][] = $t;
}
Fixed previous version of below block of code so that hidden <inputs> of previous guesses are outputted properly..
<?php
if( isset( $_POST["tmn"] ) ) {
foreach($_POST["tmn"] as $x){
echo "\n<input type ='hidden' name = 'tmn[]' value='$x'>";
}
}
?>
Updated Code:
<html>
<head>
</head>
<body>
<?php
if($_SERVER["REQUEST_METHOD"] == "POST"){
$t = $_POST["guess"];
$sayi = 80;
if($sayi >$t){
echo 'Guess higher';
}elseif($sayi == $t){
echo "You've guessed it right!<br>";
echo 'Guessed numbers: <br>';
echo implode( ", ", $_POST["tmn"] );
}else{
echo 'Guess lower';
}
if( isset( $_POST ) ) {
$_POST["tmn"][] = $t;
}
}
?>
<form method="post">
Guess the number:
<input type="number" name="guess" min ="1" max = "100"><br>
<input type="submit" name="submit">
<?php
if( isset( $_POST["tmn"] ) ) {
foreach($_POST["tmn"] as $x){
echo "\n<input type ='hidden' name = 'tmn[]' value='$x'>";
}
}
?>
</form>
</body>
</html>
For random numbers you can use rand($min, $max). to store the guesses you can use the global variable $_SESSION.
<html>
<head>
</head>
<body>
<?php
//start session (needed to use the $_SESSION variable)
start_session();
if($_SERVER["REQUEST_METHOD"] == "POST"){
//if empty -> initalize array
if (empty ($_SESSION['allguesses']){
$_SESSION['allguesses'] = array ()
}
$t = $_POST["guess"];
$sayi = 80;
if($sayi >$t){
echo 'Guess higher';
}elseif($sayi == $t){
echo "You've guessed it right!<br>";
echo 'Guessed numbers: <br>';
//echo all guesses from $_SESSION variable
foreach($_SESSION['allguesses'] as $y){
echo $y . ',';
}
}else{
echo 'Guess lower';
}
//push in $_SESSION variable
array_push($_SESSION['allguesses'],$t);
}
?>
<form method="post">
Guess the number:
<input type="number" name="guess" min ="1" max = "100"><br>
<input type="submit" name="submit">
</form>
</body>
</html>

checkbox handling php multiple checkbox

Working on a simple php code. When it press on only PH it show hello, and only on chlorine it show yello. When both is pressed it show sello.
<?php
if(isset($_POST['submit'])){
foreach($_POST['verdi'] as $animal){
if(isset($_POST['verdi[]==PH']))
{
echo "hello";
}
}
}
?>
<form name="input" action="" method="POST">
<input type="checkbox" name="verdi[]" value="PH">PH<br>
<input type="checkbox" name="verdi[]" value="Chlorine">Chlorine<br>
<br><br>
<input type="submit" name="submit" value="Submit">
</form>
You can do a simple check in PHP:
if( in_array("PH", $_POST["verdi"]) ){
echo "in array!";
}
if(isset($_POST['submit']) && is_array($_POST['verdi'])) {
$checked = array();
foreach($_POST['verdi'] as $animal) {
// you can do extra validation here.
$checked[] = $animal;
}
if(in_array("PH", $checked) && in_array("Chlorine", $checked)) {
echo "sello";
} else {
if(in_array("PH", $checked)) {
echo "hello";
} else if(in_array("Chlorine", $checked)) {
echo "yello";
}
}
}

How to count input that have value?

in my code, when we click ok button . It's will echo 3
I want to apply to count only input that have value only
How to do ?
<?php
if(isset($_POST["submit"]))
{
echo count($_POST["to_more"]);
}
?>
<form name="f1" method="post">
<input type="text" name="to_more[]">
<input type="text" name="to_more[]">
<input type="text" name="to_more[]">
<input type="submit" name="submit" value="OK">
</form>
try this
<?php
if(isset($_POST["submit"]))
{
echo count(array_filter($_POST["to_more"]));
}
?>
How about
if(isset($_POST["submit"])){
$count = 0 ;
foreach($_POST["to_more"] as $data){
if($data != '') $count++;
}
if($count > 0)
echo $count;
}
array_filter should help
<?php
$ar = isset($_POST["to_more"]) ? $_POST["to_more"] : array();
$ar = array_filter($ar, function($el){ return !empty($el);});
echo count($ar);
?>
Should be quicker than foreach, btw.
UPD: Oh, seems, NLSaini posted the precise solution, check it.

how to echo exploded array line

Here's the code.
<!DOCTYPE html>
<html>
<head><title>Numbers</title></head>
<body>
<form action="index.php" method="get">
<b>Numbers</b>
<br>
<textarea rows="12" cols="25" name="result" value="result"></textarea>
<br>
<input type="submit" value="Submit" name="submit">
</form>
</body>
</html>
<?php
$result=$_GET["result"];
if (empty($_GET['result']))
{
echo '<p><font size="3" color="red">Field is Empty*</font></p>';
}
elseif (isset($_GET['result']))
{
$result=(explode("\n", $result));
}
{
echo count ($result);
echo "<br />";
echo array_sum($result);
}
?>
Ok so I figured out how to get most of my assignment's tasks and the last 1 that I am stuck with is using similar codes such as filter_var to print out non-numerical values that are submitted. Ex. a b c * & ! #
PRINTING any invalid inputs that aren't numbers. Ex. Letters, symbols.
Any suggestions?
you can use the below code on your $result array & get the desired results:
$oddArray = array();
$evenArray = array();
$skippedArray = array();
foreach($result as $value)
{
if(is_numeric($value))
{
if($value%2 == 0)
{
$evenArray[] = $value;
}
else
{
$oddArray[] = $value;
}
}
else
{
$skippedArray[] = $value;
}
}
echo "Sum of odd values entered: ".array_sum($oddArray);
echo "Sum of even values entered: ".array_sum($evenArray);
echo "Skipped invalid values entered: ";
print_r($skippedArray);
I hope above will help you.

creating a find and replace application in php

i have a php code :
<?php
$offset=0;
if(isset($_POST['text']) && isset($_POST['searchfor']) && isset($_POST['replacewith']))
{
$text=$_POST['text'];
$search=$_POST['searchfor'];
$replace=$_POST['replacewith'];
$search_length=strlen($search);
if(!empty($text)&&!empty($search)&&!empty($replace))
{
while($strpos=strpos($text,$search,$offset))
{
$offset=$strpos + $search_length;
$text=substr_replace($text,$replace,$strpos,$search_length);
}
echo $text;
}
else
{
echo 'pls fill in all fields';
}
}
?>
<form action='string_search.php' method='POST'>
<textarea name='text' rows='6' cols='30'></textarea><br><br>
Search For:
<input type='text' name='searchfor'><br><br>
Replace with:
<input type='text' name='replacewith'><br><br>
<input type='submit' value='find and replace'>
</form>
When i type a string.Forexample 'i found my dog' in textarea and search for 'dog' in 'search for' then replace with 'cat' in 'replace' then submit it will out put 'i found my cat'.However it always output the original string ('i found my dog').I don't know why ?
Why do you use:
while($strpos=strpos($text,$search,$offset)) {
$offset=$strpos + $search_length;
$text=substr_replace($text,$replace,$strpos,$search_length);
}
echo $text;
??
I think its the same if you just use:
$strpos=strpos($text,$search);
$text=substr_replace($text,$replace,$strpos,$search_length);
echo $text;
And probably is your while who is doing the bad work. I don't know, it's a suggestion. With this, your code should work.
<?php
$offset = 0;
if (isset($_POST['user_input_box']) && isset($_POST['search']) && isset($_POST['replace'])) {
$user_input_box = $_POST['user_input_box'];
$search = $_POST['search'];
$replace =$_POST['replace'];
//To check whether above code work or not
// echo $user_input_box = $_POST['user_input_box'];
// echo $search = $_POST['search'];
// echo $replace = $_POST['replace'];
$search_length = strlen($search);
if (!empty($_POST['user_input_box']) && !empty($_POST['search']) && !empty($_POST['replace'])) {
while ($string_position=strpos($user_input_box, $search ,$offset)) {
// to check while and strpos
// echo $string_position."<br>";
// echo $offset = $string_position + $search_length;
$offset = $string_position + $search_length;
$user_input_box = substr_replace($user_input_box, $replace,$string_position,$search_length );
}
echo $user_input_box;
}
else{
echo "Please fill the complete fields.";
}
}
?>
<form method="POST" action="eleven0.php">
<textarea cols="34" rows="12" name="user_input_box"></textarea>
<br><br>
<label>Search For: </label> <br>
<input type="text" name="search"><br><br>
<label>Replace With: </label><br>
<input type="text" name="replace"><br><br>
<input type="submit" value="Find and Replace">
</form>

Categories