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

PHP – strip_tags() Replace with Spaces Example

We are going to learn php strip tags with spaces. You will learn how to replace php Strip_tags with space. In this article, we will replace php…

How to Get Duplicate Values from Array in PHP?

We will learn php array get duplicate values. we will help you to give an example of how to get duplicate values from array in php. you…

PHP explode() Function Example Tutorial

A PHP explode function example is given in this article. I will demonstrate how to use PHP’s explode string to array function. This is a basic example…

How to Convert Array to String in PHP?

We will cover the example of converting an array to a string in PHP in this lesson. We’ll examine an example of converting an array to a…

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

We will go over how to transform a string into an array in PHP in this little tutorial. Describe how to convert a string value into an…

How to Use the array_merge() Function in PHP?

We will go over the simple and quick method to use the PHP array_merge() function example in this brief tutorial. Let’s talk about the PHP example of…

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