String in PHP

10

The string is a collection of char, String contains all data using a single stream. String indexing will be similar to an array, it will start from 0 to strlen()-1, strlen() is the predefined method that will return the total number of char in String.
for example  
$x = "hello";
echo $x;
but indexing will be started from 0 to 4.
echo $x[2];   //l
in PHP We can declare String using a single quote ' ' and double quote " " both. Single quote string provides pure string value means the variable value will not be rendered. but double quote String " " provide normal string data means the variable value will be rendered.
for example
$x = 'abc ';
echo '$x';   o/p $x
$x= "abc";
echo "$x";     o/p abc
...............................................................................................................................................

Predefine function of String:-
1) strlen() :-  it return String size
   $x = "hello";
echo strlen($x);
2) strrev():-  it will reverse String
Example of String Function
  $x = "hello";
echo strrev($x);
3) strtoupper():-  Convert string into upper case 
$x = "hello";
echo strtoupper($x).
4) strtolower():-  Convert string into lower case 
$x = "hello";
echo strtolower($x).
5 )str_replace():-  We can replace particular char from String.
$x = "hello";
echo str_replace('h','m' , $x);
5) implode():-  It is used to convert array to String.
$x = array("c","c++","java","php");
echo implode(' ', $x);  //first parameter glue(symbol) and second parameter array
6)  explode():-  It is used to convert String to the array.
$s = "C,C++,JAVA,PHP,.NET,SQL";
$arr = explode(",",$s);
print_r($arr);
Complete String based program:-
<?php
$x = "hello";
echo strlen($x)."<br>";
echo strrev($x)."<br>";
echo strtoupper($x)."<br>";
echo strtolower($x)."<br>";
echo str_replace('h','m' , $x);
echo "<br>";
$x = array("c","c++","java","php");
echo implode(' ', $x);
$s = "C,C++,JAVA,PHP,.NET,SQL";
$arr = explode(",",$s);
print_r($arr);
?>
Assignments:-
1)  WAP to check that string is palindrome or not?
2)  WAP  to convert string in the opposite case?
3) WAP to validate mobile?
4)  WAP to validate email?
5)  WAP to validate the name that it should be alphabets?
6)  WAP to validate password that it is weak, strong, or medium?
weak:-  if no special char and length is less then 6
medium:-  less than two special char and length above 6
Strong:- four special char, one upper case, one number length about 8 then 

Tags

إرسال تعليق

10تعليقات

POST Answer of Questions and ASK to Doubt

  1. MANSI DUBEY



    $string="madam";

    if ($string == strrev($string)) {
    echo "string is palindrome";
    }
    else
    echo "string is not palindrome" ;

    ردحذف
  2. MANSI DUBEY



    $x="php";
    if ($x == strtolower($x) ) {
    echo strtoupper($x);
    }
    if ($x == strtoupper($x) ) {
    echo strtolower($x);
    }

    ردحذف
  3. function Palindrome($string){
    if (strrev($string) == $string){
    return 1;
    }
    else{
    return 0;
    }
    }
    $original = "DAD";
    if(Palindrome($original)){
    echo "Palindrome";
    }
    else {
    echo "Not a Palindrome";
    }

    ردحذف
  4. print"To check string is palindrome or not";

    echo "
    ";
    echo "
    ";

    $x = "MALAYALAM";

    echo $x;

    echo "
    ";
    echo "
    ";

    $y = strrev($x);

    if($x==$y) {
    echo "String [ $x ] is Palindrome";
    }

    else {
    echo "string [ $x ] is not Palindrome";
    }

    ردحذف
  5. Shubham Nandwal
    Palindrome or Not.

    $st="nayan";
    $str=strrev($st);

    if($st==$str)
    {
    echo "THIS IS PALINDROME";
    }
    else
    {
    echo "THIS IS NOT PALINDROME";
    }

    ردحذف
  6. Shubham Nandwal
    Convert String In Opposite Case.

    $st=("shiva concept");
    $stu=strtoupper($st);
    $stl=strtolower($st);
    if($st==$stu)
    {
    echo strtolower($st);
    }
    else
    {
    echo strtoupper($st);
    }

    ردحذف
  7. Shubham Nandwal
    Validate Mobile

    $st="9827212345";

    if(strlen($st)=='10')
    {
    echo "YOUR MOBILE NO. IS $st";
    }
    else{
    echo "PLEASE ENTER A VALID 10 DIGIT MOBILE NO.";
    }

    ردحذف
  8. Shubham Nandwal

    Validate E-mail Id

    $em="abcd_123@gmail.com";
    if(filter_var($em,FILTER_VALIDATE_EMAIL))
    {
    echo "YOUR E-MAIL ID IS" ,"
    ","
    ", "$em";
    }
    else
    {
    echo "PLEASE ENTER A VALID E-MAIL ID";
    }

    ردحذف
  9. Shubham Nandwal
    Name Validation(Should Be Alphabetic)

    $st = 'Shubham Nandwal';

    if(preg_match("/^[a-zA-Z ,]+$/", $st))
    {
    echo "$st";
    }
    else
    {
    echo "This Text Contents Non Alphabetic Characters";
    }

    ردحذف
  10. Shubham Nandwal
    Password Validation(Strong/Medium/Weak)

    $pass = 'Shiva*123';

    $str=strlen($pass);

    if((preg_match("/^[a-zA-Z @ # $ % * _ - 0-9]+$/", $pass)) && $str>8)
    {
    echo "Password Is Strong";
    }
    elseif(preg_match("/^[a-zA-Z @ # $ % * _ - 0-9]+$/", $pass) && $str>6)
    {
    echo "Password Is Medium";
    }
    elseif($str<6 or !preg_match("/^[a-zA-Z ,]+$/", $pass))
    {
    echo "Password Is Weak";
    }

    ردحذف
إرسال تعليق