Home > DeveloperSection > Articles > PHP String function

PHP String function


PHP PHP 
Ratings:
0 Comment(s)
 2959  View(s)
Rate this:

PHP String function

In this article I will describe some basic string function which is used in php. Let’s we have a brief idea about it.

Concatenation Operator in PHP:

The concatenation operator is used to put two string values together. Concatenation operator is define by ‘.’(Dot) sign.

Let’s we have an example, how to concatenate two string values using concatenation operator (.).

Example:

<html>

    <head>

       <title>Concatenation Operator Demo</title>

   </head>

<body>

       <?php

              // Declare first string

              $str1 = "Hello";

 

              // Declare second string

              $str2 = "word";

             

              // Concatenate str1 and str2

              $result = $str1." ".$str2;

             

              // display result

              echo $result;

       ?>

</body>

</html>

Output:

PHP String function

strlen () function :

In PHP, strlen() function is used to return the length of string.

Let’s we have an example, how to use strlen() function in php.

Syntax: strlen (string value);

Example:

<html>

    <head>

       <title>Calculate String Length </title>

   </head>

<body>

       <?php

              // Declare string type variable

              $lenDemo = "This is a demo string to calculate its length";

 

              // calculate length of string

              $calstrlen  =  strlen($lenDemo);

             

              echo "String length is:" .  $calstrlen  ;             

 

       ?>

</body>

</html>

Output:

PHP String function

 strops() function:

The strpos() function is used to search for a character/text  within a string. i.e. with the help of strpos() function  you can search a specific character or specific text string. If match is found, strops() function will returns character position of first match. If no match is found, it will return false (Nothing to display).

Syntax:  strops(string strname, value);

Let’s we have an example, how to search a character or text within a string.

Case 1: If string or character founds in given string.

Example:

<html>

    <head>

       <title>Searching string in PHP</title>

   </head>

<body>

       <?php

              // Declare the string

              $lenDemo = "This is a demo string to calculate its length";

 

              // Search character

              echo "character position is:". strpos($lenDemo, 'h')."</br>";

             

              // Search string

 

              echo "String position is:".strpos($lenDemo,"to") ;                  

       ?>

</body>

</html>

Output:

PHP String function

Case 2: If string or character doesn’t found in given string.

Example:

<html>

    <head>

       <title>Searching string in PHP</title>

   </head>

<body>

       <?php

              // Declare the string

              $lenDemo = "This is a demo string to calculate its length";

 

              // Search character

              echo "character position is:". strpos($lenDemo, 'z')."</br>";

             

              // Search string

 

              echo strpos($lenDemo,"go") ;            

 

       ?>

</body>

</html>

Output:

PHP String function

Note: Searching index start from 0.


Don't want to miss updates? Please click the below button!

Follow MindStick