Showing posts with label code. Show all posts
Showing posts with label code. Show all posts

Friday, September 18, 2015

Readmore function using explode function

<?php

$string = "Lorem ipsum dolor sit amet, consectetur adipisicing elit. Dolorum in minima necessitatibus, neque odit optio perspiciatis quasi ratione suscipit voluptates! Alias blanditiis corporis distinctio exercitationem, facere ipsa necessitatibus quo saepe.";

function read_more($string, $length = 20){
   $string_array = explode(' ', $string);
    $string_array_length = count($string_array);
    $new_string = "";
    if($length > $string_array_length){
        $length = $string_array_length;
    }
   for($i = 0; $i < $length; $i++){
     $new_string .= $string_array[$i] . ' ';
   }
    return $new_string;
}
echo read_more($string, 200);

Saturday, September 12, 2015

queue class for pushing and pop from last

<?php

class Queue {
    public $number = array();
    public $index = -1;
    public function is_empty(){
        if($this->index == -1){
            return true;
        }
        return false;
    }
    public function push($element)
    {
        $this->index++;
        $this->number[$this->index] = $element;
    }
    public function pop_from_last(){
       return array_shift($this->number);
    }

}

$person = new Queue();
$person->push(2);
$person->push(4);
$person->push(8);
$person->push(9);
print_r($person->number);
echo '<br>';
echo $person->pop_from_last();
echo '<br>';
print_r($person->number);

storing form data to a file (working with file)

<?php
if(isset($_POST['name'])){
    $name = $_POST['name'];
    $email = $_POST['email'];
    $contact = $_POST['contact'];
    $string = $name . " \t "  . $email . " \t " . $contact . "\n";
    $pointer = fopen('myfile.txt', 'a+');
    fwrite($pointer, $string);
}


?>
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
</head>
<body>
<div class="container">
    <h1>Please contact with us</h1>
    <form method="post">
       <div class="form-group">
           <label for="name">Name</label>
           <input type="text" name="name" id="name" class="form-control">
       </div>
        <div class="form-group">
            <label for="email">Email</label>
            <input type="text" name="email" id="email" class="form-control">
        </div>
        <div class="form-group">
            <label for="contact">Contact</label>
            <input type="text" name="contact" id="contact" class="form-control">
        </div>
        <div class="form-group">
            <input type="submit" name="submit" id="contact" class="btn btn-lg btn-success">
        </div>

    </form>
</div>

</body>
</html>

Tuesday, September 8, 2015

array stack and queue function in a class to demonstrate push, pop, first, last, pop from last, get element

<?php
class Stack {
    private $stack_array = array();
    private $index = -1;

    public function print_array()
    {
        print_r($this->stack_array);
    }
    public function isEmpty()
    {
        if($this->index == -1){
            return true;
        }else{
            return false;
        }
    }

    public function push($element)
    {
        $this->index++;
        $this->stack_array[$this->index] = $element;
    }

    public function pop()
    {
        if($this->isEmpty()){
            echo 'The array doesn\'t contain any elemnet';
        }else{
            $element = $this->stack_array[$this->index];
            unset($this->stack_array[$this->index]);
            $this->index--;
        }
    }

    public function pop_from_last()
    {
        if($this->isEmpty()){
            echo 'The array doesn\'t contain any elemnet';
        }else{
            $element = $this->stack_array[0];
            unset($this->stack_array[0]);
            $this->index--;
            return $element;
        }
    }
    public function first()
    {
        if($this->isEmpty()){
            echo 'The array doesn\'t contain any elemnet';
        }else{
            return $this->stack_array[$this->index];
        }
    }

    public function last()
    {
        if($this->isEmpty()){
            echo 'The array doesn\'t contain any elemnet';
        }else{
            return $this->stack_array[0];
        }
    }

    public function get_element($position)
    {
        if($position >= $this->index + 1){
           echo 'position is exceeding the array index';
        }else{
            $i = $this->index - ($position - 1);
            return $this->stack_array[$i];

        }
    }
}

$stack = new Stack();
$stack->push(8);
$stack->push(5);
$stack->push(4);
$stack->push(9);
$stack->push(2);
$stack->print_array();
echo '<br>';
echo $stack->get_element(1);
echo '<br>';
echo $stack->get_element(2);
echo '<br>';
echo $stack->get_element(3);
echo '<br>';
echo $stack->get_element(30);

Monday, September 7, 2015

Basic object oriented programming in php

<?php

function is_phone_number($number){
$length = strlen($number);
if($length == 11){

for ($i=0; $i < $length; $i++) { 
if(ord( $number[$i]) < ord('0') ||  ord( $number[$i]) > ord('9')){
return false;
}
}
return true;
}
return false;


class Person {
public $name;
public $profession;
public $phone_number;
public function check_phone_number($phone_number){
if(is_phone_number($phone_number)){
$this->phone_number = $phone_number;
}
}
}

$polo = new Person();
$polo->name = 'Polo dev';
$polo->profession = 'Student';
$polo->check_phone_number('01670978989');

echo $polo->name . '<br>'; 
echo $polo->profession . '<br>'; 
echo $polo->phone_number . '<br>'; 

array stack class to demonstrate push pop first last

<?php
class Stack {
    private $array = array(2, 3, 4, 5);
    public function print_array(){
        print_r( $this->array );
    }
    public function push_to_array($value){
        $this->array[] = $value;
    }
    public function pop_from_array(){
        $count = count($this->array) - 1;
        if(empty($this->array[$count])){
           echo 'array is empty';
        } else{
            $pop_value = $this->array[$count];
            unset($this->array[$count]);
            return $pop_value;
        }
    }
    public function last(){
        if(empty($this->array[0])){
            return 'array is empty';
        } else{
            return $this->array[0];
        }
    }
    public function first(){
        $count = count($this->array) - 1;
        if(empty($this->array[$count])){
            return 'array is empty';
        } else{
            return $this->array[$count];
        }
    }

}

$array = new Stack();
echo $array->pop_from_array();
$array->print_array();

Sunday, September 6, 2015

setcookie() function and $_COOKIE[] super global usage

<?php

if(isset($_GET['name'])){
   $name = $_GET['name'] ;
    setcookie('user_name', $name, time()+1000);
    $user_name = $name;
}

if(isset($_COOKIE['user_name'])){
$user_name = $_COOKIE['user_name'];
}
?>

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
</head>
<body>
<div class="container">
    <div class="row">
        <div class="col-md-4 col-md-offset-4">
            <br><br>
            <?php if(isset($user_name)){
                echo '<h1> Welcome ' . $user_name .'</h1>';
            }else{

             ?>
            <form method="get" action="">
               <div class="form-group text-center">
                   <label class="control-label" for="name">Name</label>
                   <input class="form-control" type="text" name="name" id="name">
               </div>
                <div class="form-group">
                    <input class="btn btn-block" type="submit" name="submit" id="name">
                </div>
            </form>
            <?php } ?>
        </div>
    </div>
</div>
</body>
</html>

Saturday, September 5, 2015

manual dictionary function in php

<?php
function lowerCase($string){
    $new_string = "";
    $length = strlen($string);
    for($i = 0; $i < $length; $i++){
        if(ord($string[$i]) >= 65 && ord($string[$i]) <= 91){
            $new_string .= chr(ord($string[$i]) + 32);
        }else{
            $new_string .= $string[$i];
        }
    }
    return $new_string;
}

function which_is_first($string1, $string2){
    $string1 = lowerCase($string1);
    $string2 = lowerCase($string2);
    $len1 = strlen($string1);
    $len2 = strlen($string2);
    if($len1 < $len2){
        $min_len = $len1;
    }else{
        $min_len = $len2;
    }
    for($i = 0; $i < $min_len; $i++){
        if($string1[$i] > $string2[$i]){
            return 2;
        }elseif($string1[$i] < $string2[$i]){
            return 1;
        }
    }
    if($len1 < $len2){
        return 1;
    }else{
        return 2;
    }

}
function dictionary($words){
    $arr_len = count($words);
    for($i = 0; $i < $arr_len; $i++){
        for($k = $i; $k < $arr_len; $k++){
            if(which_is_first($words[$i], $words[$k]) == 2 ){
                $temp = $words[$i];
                $words[$i] = $words[$k];
                $words[$k] = $temp;
            }
        }
    }
    return $words;
}
$dictionary = dictionary(['cat', 'bat', 'ball', 'home', 'he', 'she']);
print_r($dictionary);

manual string length function in php

<?php

function string_len($string){
    $i = 0;
    while(isset($string[$i])){
       $i++ ;
    }
    return $i;
}

function for lower word between 2 word - basic logic of dictionary

<?php
function lowerCase($string){
    $new_string = "";
    $length = strlen($string);
    for($i = 0; $i < $length; $i++){
        if(ord($string[$i]) >= 65 && ord($string[$i]) <= 91){
            $new_string .= chr(ord($string[$i]) + 32);
        }else{
            $new_string .= $string[$i];
        }
    }
    return $new_string;
}

function lower_word($string1, $string2){
    $string1 = lowerCase($string1);
    $string2 = lowerCase($string2);
    $len1 = strlen($string1);
    $len2 = strlen($string2);
    if($len1 < $len2){
        $min_len = $len1;
    }else{
        $min_len = $len2;
    }
    for($i = 0; $i < $min_len; $i++){
        if($string1[$i] > $string2[$i]){
            return $string2;
        }elseif($string1[$i] < $string2[$i]){
           return $string1;
        }
    }
    if($len1 < $len2){
        return $string1;
    }else{
        return $string2;
    }

}
echo lower_word('Bat', 'ball');

Thursday, September 3, 2015

manual anagram in php

<?php
function lowerCase($string){
    $new_string = "";
    $length = strlen($string);
    for($i = 0; $i < $length; $i++){
        if(ord($string[$i]) >= 65 && ord($string[$i]) <= 91){
            $new_string .= chr(ord($string[$i]) + 32);
        }else{
            $new_string .= $string[$i];
        }
    }
    return $new_string;
}
function is_anagram($string_one , $string_two){
    $string_one = lowerCase($string_one);
    $string_two = lowerCase($string_two);
    $length_string_one = strlen($string_one);
    $length_string_two = strlen($string_two);
    $length = 0;
    if ($length_string_one == $length_string_two){
        for($i = 0; $i < $length_string_one; $i++){
            for($k = 0; $k < $length_string_one; $k++){
                if($string_one[$i] == $string_two[$k]){
                    $length += 1;
                    break;
                }
            }
        }
        if($length == $length_string_one){
            return true;
        }else{
            return false;
        }
    }
}

echo is_anagram('bangladesh', 'eshbanglad');

string replace in php manually

<?php
function matching ($string, $matching_word) {
    $length_first = strlen($string);
    $length_second = strlen($matching_word);
    for($i = 0; $i < $length_first; $i++){
        for($j = 0; $j < $length_second; $j++){
            if($string[$i + $j] == $matching_word[$j]){
                if($j == $length_second - 1){
                    return  $i;
                }
            } else{
                break;
            }

        }
    }
    return -1;
}


$string = 'bangladesh is our home land';
$length_string = strlen($string);
$matching_word = 'home';
$length_matching_word = strlen($matching_word);
$replace_word = 'native';
$length_replace_word = strlen($replace_word);
$new_string = "";

$index = matching($string, $matching_word);

if($index == -1){
echo 'replace_word not found';
}else{

for($i = 0; $i < $index; $i++){
   $new_string .= $string[$i];
}
for($k = 0; $k < $length_replace_word; $k++){
   $new_string .= $replace_word[$k];
}
$index = $index + $length_matching_word;
for($l = $index; $l < $length_string; $l++){
   $new_string .= $string[$l];
}

echo $new_string;
}

manual string replace function in php - vasanth sai sir code

<?php

function getMatchIndex($orig,$find)
{
$origLen = strlen($orig);
$findLen = strlen($find);
for($i=0;$i<$origLen;$i++){
for($j=0;$j<$findLen;$j++)
{
if($i+$j < $origLen)
{
if($orig[$i+$j]==$find[$j])
{
if($j==$findLen-1)
{
return $i;
}
}
}
else
{
break;
}
}
}
return -1;
}

function replaceString($orig,$find,$rep,$index)
{
$origLen = strlen($orig);
$findLen = strlen($find);
$finalString = "";
for($i=0;$i<$index;$i++)
{
$finalString = $finalString.$orig[$i];
}
$finalString = $finalString.$rep;
for($i=$index+$findLen;$i<$origLen;$i++)
{
$finalString = $finalString.$orig[$i];
}
return $finalString;
}

function myStrRep($orig,$find,$rep)
{
$index = getMatchIndex($orig,$find);
if($index == -1)
{
echo "$find not found in $orig";
}
else
{
echo replaceString($orig,$find,$rep,$index);
}
}

myStrRep("hello","el","lo");

?>

Wednesday, September 2, 2015

DOM manipulation in php - bad practice - only for getting logic

<?php
    $heading = true;
    $paragraph = true;

    if(isset($_POST['heading'])){
        $heading = true;
        $paragraph = false;
    }

    if(isset($_POST['paragraph'])){
        $paragraph = true;
        $heading = false;
    }

    if(isset($_POST['all'])){
        $paragraph = true;
        $heading = true;
    }

?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
    <style>
        p{
            margin-top: 25px;
        }
    </style>
</head>
<body>
<div class="container">
    <div class="row">
        <div class="col-md-4">
            <form method="post" action="">
                <br><br>
                <input type="submit" name="heading" value="heading" class="btn btn-success btn-lg">
                <br><br>
                <input type="submit" name="paragraph" value="paragraph" class="btn btn-success btn-lg">
                <br><br>
                <input type="submit" name="all" value="all" class="btn btn-success btn-lg">
            </form>
        </div>
        <div class="col-md-8">
            <div id="main">
                <?php if($heading) { ?>
                <h1 id="heading">This is heading</h1>
                <?php } if ($paragraph) {?>
                <p id="paragraph">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Aliquid aspernatur assumenda at aut dolor doloremque earum inventore, iure magni nesciunt porro quis veritatis voluptate? Assumenda necessitatibus quidem reprehenderit voluptas? Accusantium? Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ad architecto aut, dicta laudantium nam quam ratione rem sequi ullam vel? Ad animi delectus est libero maxime odit reiciendis, soluta! Harum!Lorem ipsum dolor sit amet, consectetur adipisicing elit. Adipisci alias beatae deleniti dolore excepturi iste iusto maxime nesciunt nihil, nisi obcaecati placeat quae similique sit soluta! Nemo praesentium qui quod!</p>
                <?php } ?>
            </div>
        </div>
    </div>
</div>
</body>
</html>

Sunday, August 30, 2015

Basic validation in php

<?php
if($_SERVER['REQUEST_METHOD'] == "POST"){
    $errors = array();
    if(empty($_POST['name'])){
        $errors['name'] = "You have to enter name to in name field";
    }else{
        $name = $_POST['name'];
    }
    if(empty($_POST['email'])){
        $errors['email'] = "You have to enter email adddress to in email field";
    }else{
        $email = $_POST['email'];
    }
    if(empty($_POST['mobile'])){
        $errors['mobile'] = "You have to enter mobile number to in mobile field";
    }elseif(!is_numeric($_POST['mobile'])){
        $errors['mobile'] = "mobile number have to be numeric value";
    } else{
        $mobile = $_POST['mobile'];
    }
    if(empty($_POST['company'])){
        $errors['company'] = "You have to enter company to in company field";
    }else{
        $company = $_POST['company'];
    }

}

?>
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
    <style>
        .red{
           border: 2px solid #ff0000;
        }
        .green {
            border: 2px solid green;
        }
    </style>
</head>
<body>
<div class="container">
    <h1>Please Enter the each field of this form </h1>
    <form method="post" action="">
        <div class="form-group">
            <label for="name">Name</label>
            <div class="alert-danger"> <?php if(isset($errors['name'])){ echo $errors['name']; } ?> </div>

            <input value="<?php if(isset($name)){echo $name;} ?>" type="text" name="name" class="form-control <?php if(isset($errors['name'])){ echo 'red'; } ?> <?php if(isset($name)){echo 'green';} ?> " id="name" placeholder="Enter name">
        </div>
        <div class="form-group">
            <label for="email">Email</label>
            <div class="alert-danger"> <?php if(isset($errors['email'])){ echo $errors['email']; } ?> </div>
            <input value="<?php if(isset($email)){echo $email;} ?>" type="text" name="email" class="form-control <?php if(isset($errors['email'])){ echo 'red'; } ?> <?php if(isset($email)){echo 'green';} ?>  " id="email" placeholder="Enter email">
        </div>
        <div class="form-group">
            <label for="mobile">Mobile</label>
            <div class="alert-danger"> <?php if(isset($errors['mobile'])){ echo $errors['mobile']; } ?> </div>
            <input value="<?php if(isset($mobile)){echo $mobile;} ?>" type="text" name="mobile" class="form-control <?php if(isset($errors['mobile'])){ echo 'red'; } ?>  <?php if(isset($mobile)){echo 'green';} ?> " id="mobile" placeholder="Enter mobile">
        </div>
        <div class="form-group">
            <label for="company">Company</label>
            <div class="alert-danger"> <?php if(isset($errors['company'])){ echo $errors['company']; } ?> </div>
            <input value="<?php if(isset($company)){echo $company;} ?>" type="text" name="company" class="form-control <?php if(isset($errors['company'])){ echo 'red'; } ?> <?php if(isset($company)){echo 'green';} ?>  " id="company" placeholder="Enter company">
        </div>
        <button type="submit" name="" class="btn btn-default">Submit</button>
    </form>
</div>

</body>
</html>

findings frequency number from a file by using own explode code and frequency code - modularity approach

in functions.php
===========================

<?php
//for exploding a string to array
function myExplode($string)
{
    $length = strlen($string);
    $word = "";
    $words = array();
    for ($i = 0; $i < $length; $i++) {
        if (ord($string[$i]) == 13) {
            continue;
        }
        if ($string[$i] == " " || $i == $length - 1 || $string[$i] == "\n") {

            if ($i == $length - 1) {
                $word .= $string[$i];
            }
            if(!empty($word)){
                $words[] = $word;
            }
            $word = "";
        } else {
            $word .= $string[$i];
        }
    }
    return $words;
}




//for determine frequency
function frequency($words){
    $frequency = array();

    foreach ($words as $word) {
        if(array_key_exists($word, $frequency)){
            $frequency[$word] ++;
        } else {
            $frequency[$word] = 1;
        }
    }

    return $frequency;
}


in file.php
================

<?php
require 'functions.php';
$pointer = fopen('php.txt', 'r');
$string = fread($pointer, filesize('php.txt'));
$words = myExplode($string);
$frequency = frequency($words);
print_r($frequency);

findings frequency number from a file by using own explode code and frequency code

<?php
$pointer = fopen('php.txt', 'r');
$string = fread($pointer, filesize('php.txt'));
$length = strlen($string);
$word = "";
$words = array();

for($i = 0; $i < $length; $i++){
    if(ord($string[$i]) == 13){
            continue;
        }
    if($string[$i] == " " || $i == $length - 1 || $string[$i] == "\n"){

        if($i == $length - 1){
            $word .= $string[$i];
        }
        if(!empty($word)){
            $words[] = $word;
        }
        $word = "";
    }else{
        $word .= $string[$i];
    }
}
//for determine frequency
$frequency = array();

foreach ($words as $word) {
    if(array_key_exists($word, $frequency)){
        $frequency[$word] ++;
    } else {
        $frequency[$word] = 1;
    }
}

foreach($frequency as $key => $value){
    echo "$key => $value <br>";
}

Friday, August 28, 2015

array in descending order in php

<?php
$array = [2, 5, 7, 99, 90, 42, 88, 8, 9, 0, 56];
$count = count($array);
for($i = 0; $i < $count; $i++){
    for($j = 0; $j < $count-1; $j++){
        if($array[$j] < $array[$j+1]){
           $temp = $array[$j+1];
            $array[$j+1]= $array[$j];
            $array[$j] = $temp;
        }
    }
}

for($k = 0; $k < $count; $k++){
    echo $array[$k] . '<br>';
}

sorting ascending order manually in php

<?php
$array = [2, 5, 7, 99, 90, 42, 88, 8, 9, 0, 56];
$count = count($array);
for($i = 0; $i < $count; $i++){
    for($j = 0; $j < $count-1; $j++){
        if($array[$j] > $array[$j+1]){
           $temp = $array[$j+1];
            $array[$j+1]= $array[$j];
            $array[$j] = $temp;
        }
    }
}

for($k = 0; $k < $count; $k++){
    echo $array[$k] . '<br>';
}

programming glossary

lazy evaluation,
DRY - don't repeat yourself

css snippet for blogger code highlighting

code, .code {     display: block;     background: beige;     padding: 10px;     margin: 8px 15px; }