Ex No: 11 PHP String Functions
Aim: To perform String Operations using php
Algorithm
Step1: Design a webpage to get string values and options for string operations.
Step2: On form submit check if String and String1 elements are set and not empty
   A) Get string value and sting operation options selected from POST method.
   B) Check if Single String operation options are selected
     i) Get the string value.
    ii) Use foreach loop to get selected options.
                    a) Call Single String Function with String value and Selected Option.
   C) Check if Two String operation options are selected.
     i) Get two string value.
    ii) Use foreach loop to get selected options.
                    a) Call Two String Function with String values and Selected Option.
function single_string($str,$opr)
Step 1: Switch based on $opr value
         A) For option "len": Calculate string length and display the result
         B) For option "rev": Reverse the string and display the result.
         C) For option "expimp":
                  i) Calculate string explode and display the result using foreach loop.
                  ii) Calculate string implode using "_" and display the result.
         D) For option "search": Search "o" within the given string and display the result
         E) For option "strrep": Replace "l" with "a" in the given string and display the result.
function two_string($str1, $str2, $opr)
Step 1: Switch based on $opr value
         A) For option "comp": Compare the two strings and display the result.
         B) For option "sim": Find similarity between the two strings and display the result.

index.html


<!DOCTYPE html>
<html>
<head>
 <meta charset="utf-8">
 <title>String Operations</title>
</head>
<body>
 <h3><u>String Operations</u></h3>
 <div style="float: left; width: 500px;">
  <form id="strprocess" name="strprocess" method="POST" action="ex11.php">
   <table>
    <tr><td colspan="2"><b>Single String Operations</b></td></tr>
    <tr>	  
     <td>Enter String</td>
     <td><input type="text" name="str"/></td>
    </tr>
    <tr>
     <td>Operations</td>	
     <td>
       <input type="checkbox" name="singstr[]" value="len">String Length<br>
       <input type="checkbox" name="singstr[]" value="rev">String Reversal<br>
       <input type="checkbox" name="singstr[]" value="expimp">Explode(space) and Implode(_)<br>
       <input type="checkbox" name="singstr[]" value="search">Search First occurance of "o"<br>
       <input type="checkbox" name="singstr[]" value="strrep">Replace "l" with "a"<br>
     </td>
    </tr>
    <tr>
     <td colspan="2"><b>Two Sting Operations</b></td>	
    </tr>
    <tr>
     <td>Enter First String</td>	
     <td><input type="text" name="str1"/></td>
    </tr>
    <tr>
     <td>Enter Second String</td>	
     <td><input type="text" name="str2"/></td>
    </tr>
    <tr>
     <td>Operations</td>	
     <td>
       <input type="checkbox" name="twostr[]" value="comp">Compare<br>
       <input type="checkbox" name="twostr[]" value="sim">Check Similarity<br>
     </td>
    </tr>
    <tr>
     <td><input type="submit" value="Calculate"/></td>	
     <td><input type="reset"/></td>
    </tr>
 </table>
 </form>
 </div> 
</body>
</html>

ex5.php


<!DOCTYPE html>
<html>
<head>
 <meta charset="utf-8">
 <title>String Operations</title>
</head>
<body>
 <?php
  #single String Operations
  function single_string($str,$opr)
  {
   $res="";
   switch($opr)
   {
    case "len":
        $len = strlen($str);
        $res = "<p><b>String Length:</b> $len</p>";
        break;
    case "rev":
        $rev = strrev($str);
        $res = "<p><b>Reversed String:</b> $rev</p>";
        break;
    case "expimp":
        $exp = explode(" ",$str);
        $res = "<p><b>Exploded String:</b></p>";
        foreach($exp as $x)
         $res = $res."$x <br>";
        $imp = implode("_",$exp);
        $res = $res."<p><b>Implode String:</b> $imp";
        break;
    case "search":
    	$search = strchr($str,"o");
    	$res = "<p><b>First occurence of \"o\": </b> $search</p>";
    	break;
    case "strrep":
    	$strrep = str_replace("l","a",$str,$num);
    	$res = "<p><b>Replace \"l\" with \"a\": </b>$strrep Count: $num</p>";
    	break;
   }
   return $res;
  }
  
  #function to perform two string operration
  function two_String($str1, $str2, $opr)
  {
  	switch($opr)
  	{
	 case "comp":	 	
	 	$cmp =(strcmp($str1,$str2)==0?"Same":"Different");
	 	$res = "<p><b>Result of comparing Two String <u>$str1, $str2</u> is:</b> $cmp</p>";
	 	break;
	 case "sim":
	 	similar_text($str1,$str2,$sim);
	 	$res = "<p><b>Similarity between <u>$str1, $str2</u> is:</b> $sim</p>";
	 	break;
	}
    return $res;
  }
  #starting point of the php program
  if((isset($_POST["str"]) && $_POST["str"]!="") or ( isset($_POST["str1"]) && $_POST["str1"]!=""))
  {
   if(isset($_POST["singstr"]))
   {
    $val = $_POST["str"];
    echo "<h1><u>Single String Operations</u></h1>";
    foreach($_POST["singstr"] as $op)
    {
     $result = single_string($val,$op);
     echo $result;
    }
   }
   if(isset($_POST["twostr"]))
   {
    echo "<h1><u>Two String Operations</u></h1>";
    $str1 = $_POST["str1"];
    $str2 = $_POST["str2"];
    foreach($_POST["twostr"] as $op)
    {
     $result = two_String($str1,$str2,$op);
     echo $result;
    }
   }
  }
 ?>
 </body>
</html>

                  
Result: Thus a webpage is designed using php to perform String Operations.