String Functions in PHP?

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps!

We spend hours scrolling social media and waste money on things we forget, but won’t spend 30 minutes a day earning certifications that can change our lives.
Master in DevOps, SRE, DevSecOps & MLOps by DevOps School!

Learn from Guru Rajesh Kumar and double your salary in just one year.


Get Started Now!

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

PHP – strip_tags() Replace with Spaces Example

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps! We spend hours scrolling social media and waste money on things we forget, but won’t spend 30…

Read More

How to Get Duplicate Values from Array in PHP?

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps! We spend hours scrolling social media and waste money on things we forget, but won’t spend 30…

Read More

PHP explode() Function Example Tutorial

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps! We spend hours scrolling social media and waste money on things we forget, but won’t spend 30…

Read More

How to Convert Array to String in PHP?

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps! We spend hours scrolling social media and waste money on things we forget, but won’t spend 30…

Read More

How Can I Convert a String in PHP Into an Array?

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps! We spend hours scrolling social media and waste money on things we forget, but won’t spend 30…

Read More

How to Use the array_merge() Function in PHP?

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps! We spend hours scrolling social media and waste money on things we forget, but won’t spend 30…

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