String Functions in PHP?

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?
?>

Related Posts

Associative Array in PHP?

An associative array is a simple and very important array of PHP. This array is also used the most, it is very easy to understand this array,…

How to work in functions PHP?

If there is any task in your program which you train to execute repeatedly then instead of writing the code for that task at a different place…

For Loofs in PHP

In for-loop initialization, condition and increment happen on the same line. The loop keeps on repeating as long as the condition is true. When the for loop…

If Else Conditionals in Php?

If Else statement is the most important feature of any programming language, PHP is used to run any Code Of Block according to If Else conditions. This…

What are operators in PHP? PHP – Operator Types

Operators are used to developing data. In Programming Languages, Operators are taken from Mathematics and Operator Programmers work with Data. An operator is a chain of symbols…

What is the Variable And how does it work?

The variable is seen here as a container, which you can use to store data information. It doesn’t make any difference to him, so it becomes easy…

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