التخطي إلى المحتوى الرئيسي

String in PHP


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 

تعليقات

  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";
    }

    ردحذف

إرسال تعليق

POST Answer of Questions and ASK to Doubt

المشاركات الشائعة من هذه المدونة

Uncontrolled form input in React-JS

  Uncontrolled form input in React-JS? If we want to take input from users without any separate event handling then we can uncontrolled the data binding technique. The uncontrolled input is similar to the traditional HTML form inputs. The DOM itself handles the form data. Here, the HTML elements maintain their own state that will be updated when the input value changes. To write an uncontrolled component, you need to use a ref to get form values from the DOM. In other words, there is no need to write an event handler for every state update. You can use a ref to access the input field value of the form from the DOM. Example of Uncontrolled Form Input:- import React from "react" ; export class Info extends React . Component {     constructor ( props )     {         super ( props );         this . fun = this . fun . bind ( this ); //event method binding         this . input = React . createRef ();...

JSP Page design using Internal CSS

  JSP is used to design the user interface of an application, CSS is used to provide set of properties. Jsp provide proper page template to create user interface of dynamic web application. We can write CSS using three different ways 1)  inline CSS:-   we will write CSS tag under HTML elements <div style="width:200px; height:100px; background-color:green;"></div> 2)  Internal CSS:-  we will write CSS under <style> block. <style type="text/css"> #abc { width:200px;  height:100px;  background-color:green; } </style> <div id="abc"></div> 3) External CSS:-  we will write CSS to create a separate file and link it into HTML Web pages. create a separate file and named it style.css #abc { width:200px;  height:100px;  background-color:green; } go into Jsp page and link style.css <link href="style.css"  type="text/css" rel="stylesheet"   /> <div id="abc"> </div> Exam...

JDBC using JSP and Servlet

JDBC means Java Database Connectivity ,It is intermediates from Application to database. JDBC has different type of divers and provides to communicate from database server. JDBC contain four different type of approach to communicate with Database Type 1:- JDBC-ODBC Driver Type2:- JDBC Vendor specific Type3 :- JDBC Network Specific Type4:- JDBC Client-Server based Driver  or JAVA thin driver:- Mostly we prefer Type 4 type of Driver to communicate with database server. Step for JDBC:- 1  Create Database using MYSQL ,ORACLE ,MS-SQL or any other database 2   Create Table using database server 3   Create Form according to database table 4  Submit Form and get form data into servlet 5  write JDBC Code:-     5.1)   import package    import java.sql.*     5.2)  Add JDBC Driver according to database ide tools     5.3)  call driver in program         ...