Course Banner


PHP String function

String in PHP is a set of character sets which we can manipulate and show in our own way.

There are few inbuilt PHP functions for String only.

Here is the list

● STR_LEN

This function finds string length in character.

Example :

< ?php
  1. if($_POST){
  2. $txt=$_POST['comment'];
  3. total_words = strlen($txt);
  4. if($total_words<50){
  5.       echo"Sorry your description is too short.You have < strong>typed ".$ total_words.' Characters.< /strong> Total Characters < strong>must be greater than 50< /strong>';
  6. }
  7. }
    ?>

<br>
< strong>Type Your text : < /strong> <formname>="form1"method="post"action=" ">
< p>
< textarea name="comment"id="textarea"cols="45"rows="5"< /textarea>
< /p>
< p>
< inputtype="submit"name="button"id="button"value="Post Now"> < /p>
< /form>



After submitting the form you will see this.

● SUBSTR

Substr function is helpful in truncating any string. This function requires three parameters. First, string, starting point and third up to how many characters.

Example :

< ?php
  1. if($_POST){
  2. $txt = $_POST['comment'];
  3. echosubstr($txt,0,100);
  4. $cnt=strlen($txt);
  5. if($cnt>100){
  6.       echo"< strong>....Read More< /strong>";
  7. }
  8. }
  9. ?>

<br>
< strong>Type Your text : < /strong> <formname>="form1"method="post"action=" ">
< p>
< textarea name="comment"id="textarea"cols="45"rows="5">
Share all your special moments on Skype, whenever you want, wherever you are – all from your tablet.
Share all your special moments on Skype, whenever you want, wherever you are – all from your tablet.
Share all your special moments on Skype, whenever you want, wherever you are – all from your tablet.
Share all your special moments on Skype, whenever you want, wherever you are – all from your tablet.
< /textarea>
< /p>
< p>
< inputtype="submit"name="button"id="button"value="Post Now"> </p>
< /form>

Result :

● STR_REPLACE

Example :

< ?php
  1. if($_POST){
  2. $phrase = <$_POST['comment'];
  3. /*huffy angry*/
  4. $healthy = array("shit", "huffy", "awsome");
  5. $yummy = array("s***t", "h***y", "Great");
  6. echo $newphrase = &str_replace($healthy, $yummy, $phrase);
  7. }
  8. ?>

<br>
< strong>Type Your text : < /strong> <formname>="form1"method="post"action=" ">
< p>
< textarea name="comment"id="textarea"cols="45"rows="5">< ?php echo'THis is shit and and dont be huffy. Its awsome words'?>
< /textarea>
< /p>
< p>
< inputtype="submit"name="button"id="button"value=>"Post Now"
< /p>
< /form>



● UCFIRST

This function makes the first character of any string to Upper case.

Example :

< ?php
  1. $f_name='arpita biswas';
  2.  echoucfirst($f_name);
  3. ?>


● Strtoupper

This function converts whole string into upper case.

Example :

< ?php
  1. $name ="Hello World";
  2.       echostrtoupper($name);
  3. ?>


● Strtolower

As contrary to strtoupper, this function converts whole string into lower case.

Example :

< ?php
  1. $name ="Hello World";
  2.       echostrtolower($name);
  3. ?>


Explode

Explode function is PHP is a very useful where one can convert the string having fixed separator into an array and then can display that in one’s own format.

Explode function takes two parameters. First the separator and in second it takes string.

Example:

< ?php
  1. $colors ='Red,Green, Orange,Yellow';
  2. $ex = explode(',',$colors);
  3. print"< pre>";
  4. print_r($ex);
  5. ?>


Now we will show the array value into dropdown.

Let’s take an example:

< ?php
  1. $colors ='Red,Green, Orange,Yellow';
  2. $ex = explode(',', $colors);
  3.  echo"< select>";
  4. for($i=0; $i< count($ex); $i++){
  5.  echo"< option value='".$ex[$i]."'>".$ex[$i]."< /option>";
  6. }
  7. echo"< /select>";
  8. ?>


Suppose you are developing an e-commerce website and admin types color names of the product in comma separated value. You can store the value in a single field as comma separated and later you can use that as dropdown value after exploding.

Implode

Opposite to explode there is imploding function. In implode function; you can change separator characters into your own.

Example:

< ?php
  1. $array = array('First Name', 'Last Name', 'Email', 'Phone');
  2. $impl = implode("/", $ array);
  3.       echo$ impl;
  4. ?>

Result: