Showing posts with label php tutorial. Show all posts
Showing posts with label php tutorial. Show all posts

Wednesday, June 24, 2015

Hide email address in PHP with asterisk (*)

PHP code shows how to replace email address with asterisk(*). The script used for partially hide email address with asterisk (*).
<?php

/**
 * Hide email address with asterisk(*) 
 *
 * @param    $email
 * @reeturn  $email with asterisk(*) // t***********l@g*****l.com 
 *
 */
function hideEmail($email)
{
 $mail_segments = explode("@", $email);
    $mail_segments[0] = substr($mail_segments[0], 0, 1) . str_repeat("*", strlen($mail_segments[0]) - 2) . substr($mail_segments[0], -1);
    $pos = strpos($mail_segments[1], '.');
    $mail_segments[1] = substr($mail_segments[1], 0, 1) . str_repeat("*", strlen($mail_segments[1]) - $pos+1) . substr($mail_segments[1], $pos-1);
 return implode("@", $mail_segments);
}

/** call function and display result **/
echo hideEmail("testingdhaval@gmail.com");
?>

Wednesday, July 17, 2013

Get selected value of dropdownlist

<!DOCTYPE html>
<html>
    <head>
        <script>
            function selectedValue()
            {
                var index = document.getElementById("myValue").selectedIndex;
                var value = document.getElementById("myValue").options[index].text;
                alert(value);
            }
        </script>
    </head>
    <body>
        <form>
            Select your favorite Subject:
            <select id="myValue">
                <option>PHP</option>
                <option>HTML</option>
                <option>JQuery</option>
                <option>Javascript</option>
            </select>
        </form>
        <button type="button" onclick="selectedValue()">Selected Value</button>
    </body>
</html>

Thursday, July 4, 2013

convert stdClass Object to Array in php

<?php

/**
 * Convert an stdClass object to an array
 *
 * @param    stdClass object  $varObj The object to convert
 * @reeturn  array
 *
 */
function convertObjectToArray($varObj) {
    if (!is_object($varObj) && !is_array($varObj)) {
        return $object;
    }
    if (is_object($varObj)) {
        $varObj = get_object_vars($varObj);
    }
    return array_map('objectToArray', $varObj);
}
/** convert the object to array **/
$resultArray = convertObjectToArray( $obj );

/** Display the result array **/
print_r( $resultArray );
?>

Tuesday, June 11, 2013

Find Prime numbers between given range

Question: We have to ask user to enter number1 and number2 he/she wants, and find the prime numbers between that range which is entered by users?

Answer: Copy the below code and paste it into "your.php" file. After done this you will just need to run "your.php" file in browser and you will get the output successfully.

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
        <title>Find Prime numbers</title>
    </head>

    <body>
        <h1><center>Find Prime numbers</center></h1>

        <form action="<?php echo $_SERVER['PHP_SELF']; ?>"  method="post">
            Enter Value1:<input type="text" name="min">
            Enter Value2:<input type="text" name="max">
            <input type="submit" name="submit" value="OK">
        </form>

        <?php
        if (isset($_POST['submit'])) {
            $min = $_POST['min'];
            $max = $_POST['max'];
            if ($min == $max) {
                echo "Enter different value ";
            } else {
                if ($min > $max) {
                    $t = $min;
                    $min = $max;
                    $max = $t;
                }

                function get_prime($min, $max) {
                    $primes = array();
                    for ($x = $min; $x <= $max; $x++) {
                        if ($x == 2) {
                            $primes[] = $x;
                        }
                        for ($i = 2; $i < $x; $i++) {
                            $r = $x % $i;
                            if ($r == 0) {
                                break;
                            }
                            if ($i == $x - 1) {
                                $primes[] = $x;
                            }
                        }
                    }
                    if ($primes == NULL) {
                        echo "No prime numbers found";
                    }  else {
                        echo "Total ". count($primes) ." prime numbers:";
                        echo implode(",", $primes);
                    }
                    
                }

                get_prime($min, $max);
            }
        }
        ?>

    </body>
</html>

Monday, June 10, 2013

Prime number program

Question: We have to ask user to enter number he/she wants, and give output whether its prime number or not?

Answer: Copy the below code and paste it into "your.php" file. After done this you will just need to run "your.php" file in browser and you will get the output successfully.
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
        <title>Prime number</title>
    </head>

    <body>
        <h1><center>Prime number</center></h1>

        <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
            Enter number:
            <input type="text" name="num" />
            <input type="submit" name="submit" value="OK" />
        </form>

        <?php
        if (isset($_POST['submit'])) {
            $num = $_POST['num'];
            if ($num == 0) {
                echo $num . " is special number";
            } elseif ($num == 1) {
                echo $num . " is special number";
            } else {
                $c = 0;
                for ($i = 2; $i < $num; $i++) {
                    if ($num % $i == 0) {
                        $c++;
                        break;
                    }
                }
                if ($c) {
                    echo $num . " is not prime number";
                }
                else
                    echo $num . " is prime number";
            }
        }
        ?>
    </body>
</html>

Friday, June 7, 2013

Leap year program

Question: We have to ask user to enter year he/she wants, and give output whether its leap year or not?

Answer: Copy the below code and paste it into "your.php" file. After done this you will just need to run "your.php" file in browser and you will get the output successfully.

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
        <title>leap year</title>
    </head>

    <body>
        <h1><center>leap year</center></h1>

        <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
            Enter the year in four digit:
            <input type="text" name="year" />
            <input type="submit" name="submit" value="OK" />
        </form>
        <?php
            if (isset($_POST['submit'])) {
                $yr = $_POST['year'];
                if ($yr % 4 == 0) {
                    if ($yr % 100 == 0) {
                        if ($yr % 400 == 0) {
                            echo $yr . " is leap year";
                        }
                        else
                            echo $yr . " is not leap year";
                    }
                    else
                        echo $yr . " is leap year";
                }
                else
                    echo $yr . " is not leap year";
            }
        ?>
    </body>
</html>

Tuesday, June 4, 2013

Palindrome number or string

Question: We have to ask user to enter number or string he/she wants, and give output is number/string Palindrome or not?
(abcba-Palindrome String)
(abccba-Palindrome String)
(12321-Palindrome Number)
(123321-Palindrome Number)

Answer: Copy the below code and paste it into "your.php" file. After done this you will just need to run "your.php" file in browser and you will get the output successfully.
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
        <title>Palindrome number or string</title>
    </head>

    <body>
        <h1><center>Palindrome number or string</center></h1>

        <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
            Enter number or string:<input type="text" name="num"/>
            <input type="submit" name="submit" value="OK" />
        </form>
        <?php
        if (isset($_POST['submit'])) {
            $num = $_POST['num'];
            $numeric = is_numeric($num);

            function is_palindrome(&$str) {
                for ($i = 0; $i < strlen($str) / 2; $i++) {
                    if ($str[$i] != $str[strlen($str) - 1 - $i])
                        return false;
                }
                return true;
            }

            if (!$numeric) {
                $result = is_palindrome($num);
                if ($result) {
                    echo $num . " is palindrome string";
                }
                else
                    echo $num . " is not palindrome string";
            } else {
                $sum = 0;
                $temp = $num;
                while ($temp >= 1) {
                    $r = $temp % 10;
                    $temp = $temp / 10;
                    $sum = $sum * 10 + $r;
                }

                if ($sum == $num) {
                    echo $num . " is palindrome number";
                }
                else
                    echo $num . " is not palindrome number";
            }
        }
        ?>
    </body>
</html>

Monday, June 3, 2013

Reverse number

Question: We have to ask user to enter number he/she wants, and number should be generated reverse accordingly.
(if user enter number 12345 then reverse should be generated i.e.- 54321)

Answer: Copy the below code and paste it into "your.php" file. After done this you will just need to run "your.php" file in browser and you will get the output successfully.
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
        <title>Reverse number</title>
    </head>

    <body>
        <h1><center>Reverse number</center></h1>

        <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
            Enter number more than one digit:<input type="text" name="rev" />
            <input type="submit" name="submit" value="OK" />
        </form>
        <?php
            if (isset($_POST['submit'])) {
                $rev = $_POST['rev'];
                while ($rev >= 1) {
                    $r = $rev % 10;

                    $rev = $rev / 10;

                    echo $r;
                }
            }
        ?>
    </body>
</html>

Sunday, June 2, 2013

Fibonacci Series

Question: We have to ask user to enter the no. of terms he/she wants, and series should be generated accordingly.

Answer: Copy the below code and paste it into "your.php" file. After done this you will just need to run "your.php" file in browser and you will get the output successfully.
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
        <title>Fibonancy series</title>
    </head>
    <body>
        <h1><center>Fibonancy series</center></h1>
        <?php
            $a = 0;
            $b = 1;
            $sum = 0;
        ?>
        <form action="<?php echo $_SERVER['PHP_SELF']; ?>"  method="post">
            Enter number of terms:<input type="text" name="name">
            <input type="submit" name="submit" value="OK"><br>
        </form>
        <?php
            if (isset($_POST['submit'])) {
                $term = $_POST['name'];
                echo "$a,$b,";
                while ($term - 2) {
                    $sum = $a + $b;
                    $a = $b;
                    $b = $sum;
                    $term--;
                    echo $sum . ",";
                }
            }
        ?>
    </body>
</html>