After knowing the strings basics it’s time to know what type of operations we can do with strings.

There are many strings operations we can do…

  • Write a PHP code, for getting any string length & word count.

File – code1.php

<?php
$str=”shimanta das microcodes”;
$len=strlen($str);
echo “string length is : $len”;
$word=str_word_count($str);
echo “<br> string total words $word”;
?>
output

Note: strlen() function helps s to know string’s length/size. str_word_count() function helps to know the how many words contains inside a string.

  • Write a PHP code, to reverse a string.

File – code2.php

<?php
$str=”microcodes”;
$rev=strrev($str);
echo “string is: $str”;
echo “<br>reverse string is: $rev”;
?>
output

Note: strrev() function is used to reverse a string.

  • Write a PHP code, find a substring inside a full string.

File – code3.php

<?php
$str=”i am coder”;
echo strpos($str,”coder”);
echo ‘<br>’;
echo strpos(“rust will replace C”,”C”);
?>
output
  • Write a PHP code, to replace a substring.

Syntax – str_replace(‘select_replace_word’,’replace_word’,variable/full string)

File – code4.php

<?php
$str=”shimanta das owner of microcodes”;
echo “<br>string before: $str”;
$str = str_replace(“microcodes”,”microcodes.in”,$str);
echo “<br>string now: $str”;
?>
output