PHP  String

PHP - a string is a sequence of characters, like '34ram?'. string in php language can be as large as 2gb.

strings are useful for holding data that can be represented in text form.the string literal value enclosed in single(') or double(") quotes.

syntax:

         $str = 'my dream like a moon 1212';
         $str = "i like pizza";

you can put any type of data within single and double quotes.

       $string = "string value  232323232 $$ -%% -^343333" ;

the simplest way to specify a string is to enclose it in single quotes( 'w3webtutorial' ).

     <?php       $str1 = 'i like 121212';  $str2 = 'i like "fruit" ';
     
                    $str3 = 'i like" apple " orange  bananan  what you like';
                    
                    $str4 = "name 'w3webtutorial.com'";
                    
                    echo($str1); echo "<br/>";  
                    
                    echo($str2);    echo "<br/>";  
                    
                    echo($str3);    echo "<br/>";
                    
                    echo($str4);    echo "<br/>";
                    
           ?>           
           

string with in double quotes(""):

if the string is enclosed in double-quotes, php will interpret more escape sequences for special characters:

note : when a string is specified in double quotes, variables are parsed within it.

     <?php     $str = "sandeep kumar nehra";  $str1 = 'ankit kumar';
     
                  echo "my name is $str <br/>"; 
                  
                  echo "my name is ${str}"; 
                  
                  echo "<br/> my name is {$str}";
                  
                  echo "<br/> my name is $str1";  
                  
                 
     ?>

PHP String length

the strlen() method returns the length of a string in characters.

   <?php    $str = "i like fruit";
   
               echo "number of characters in str =". strlen( $str );
               
               echo "string length =". strlen( "sandeep kumar nehra" );
   ?>

Searching strings

the strpos() method finds the position of the first occurrence of a string inside another string.

the strrpos() method finds the position of the last occurrence of a string inside another string.

the strpos() and strrpos() methods are case-sensitive.

syntax:

         strpos( string, find, start)
         strrpos( string, find, start)
  • string :- required. specifies the string to search
  • find :- required. the find argumentspecifies the string to find
  • start :- optional. specifies where to begin the search
   <?php
              $str ="i like number of fruits you like or not?";
              
              $position = strpos( $str, "like");
              
              echo  "$position <br/>";
              
              echo  strpos( $str, "like", 19);
              
              echo  "<br/>". strrpos( $str, "like");
   ?>

uppercase and lowercase

the strtolower() function converts a string to lowercase.

the strtoupper() function converts a string to uppercase.

both functions are binary-safe.

syntax:

        strtolower( string )
        strtoupper( string )
   <?php
               $str = "i like the value";
               
               echo  strtolower( $str );  echo "<br/>";
               
               echo  strtoupper( "i like the value");
   ?>

PHP - trimming whitespace from string

the php string object trim() method removes white space from the beginning and end of a string

the ltrim() method removes white space only from the left side of a string

the rtrim() method removes white space only from the right side of a string

   trim( string, char );
<?php
         $str = "   this is ram     \n\n";
             
      echo  "before trim string length =". strlen($str);
             
      echo  "<br/> after trim  string length =". strlen(trim($str));
             
      echo  "<br/> string length after remove left white space". strlen(ltrim($str));
             
      echo  "<br/> string length after remove right white space". strlen(rtrim($str)) 
?>