Wednesday, 15 July 2015

PHP String Functions

A  string is sequence of characters, where a character is the same as a byte,hence php support 256 character set.
  •  Split function, split the string function tutorial into an array using regular expression and return the array.
  • Explode function , it is used to split a string using another sting.
 PHP strlen() function:
  •  The strlen() function is used to return the length of a string  in characters.
  • Let’s take a example to find the length of our string “Hello happy!”:
<?phpecho strlen(“Hello happy!”);?>
  • Output will be:
12
Note: The length of a string is used in loop or other php string functions, when it is important to know when the string ends i.e. in a loop, we would want to stop the loop after the last character in the string.

PHP strpos() function:

  • The strpos() function is used to search for a string or specified characters within a string.
  • If a match is found, it will return the character position of the first match. If there is  no match is found, it will return FALSE.
  • Let’s take a example to find a string ” happy!”in our string:

<?php ;echo strlen(“Hello happy!”);    ?>
  • Output will be:
6
Note: The position of a string “happy” in our string is 6 position.The reason that it is 6 position, and not 7 position, is that the first position in the string is 0  not 1.

No comments:

Post a Comment