String Functions in PHP?

Posted by

When many characters are written together in sequence, they form a string. For example, India is a string. It is made up of i, n, d, i, and characters. A string can be made up of the following elements.

  • Letters – A string can be made up of small letters (a – z) or capital letters (A – Z).
  • Numbers – A string can contain any number from 0 to 9.
  • Special Characters – A string can also contain special characters (@#$%^&!). Escape sequence characters are used to display special characters as strings.

Syntax:-

$string-name = "string";

PHP String Functions

There are many string functions already built into PHP. All these string functions are known as PHP readymade functions. We can also call the PHP String function a built-in function. We can use the functionality of the functions which are already created in PHP and by using them, we call such functions a built-in function or readymade function.

List of String Functions

  1. strlen()                               To get the length of the string.
  2. Str_words_count()             How many words are there in a string?
  3. PHP strrev()                       To reverse the string.
  4. str_replace()                      To convert strings.
  5. strpos():-                            To get the position number of the word in the string.
  6. str_repeat                          The str_repeat() function repeats a string a specified number of times.

PHP strlen() function

<?php
     echo strlen("Learn PHP strlen function"); // outputs 25
?>

String word count function: str_word_count() –

<?php
    echo str_word_count("Learn PHP str_words_count function"); // output 34
?>

String reverse function: strrev()-

<?php
     echo strrev("Learn strrev function")// outputs : noitcnuf verrts nrael
?>

String replace function: str_replace() –

<?php
   echo str_replace("Hello", "HI", "Hello ,I am learning PHP"); 
   // outputs:- HI ,I am learning PHP
?>

String position function: strpos():-

<?php
    echo strpos("How are you ?", "You "); // outputs 8
?>

str_repeat 

<?php
    echo str_repeat("How are you?",  4 ); 
    // outputs : How are you?How are you?How are you?How are you?
?>

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x