PHP Strings

In this PHP tutorial, you are going to learn about various string functions in PHP. Along with that you are also going to learn about the string manipulation functions in PHP.

Strings in PHP

String is a data type that consist of a series of characters, or simply strings can be referred as a character array.

Strings are created when you declare variable and assign it a character. You can directly print it using echo function.

Ex: $b = "bob";
echo"$b" ;

We can perform several functions on string in PHP . The string manipulation functions are.

Various String Manipulation Functions in PHP

  • strlen( ) :
    This is used to find the length of the string . It gives output how many characters do the string has
  • str_word_count( ) :
    To find the number of words in a string . It gives number of words that the given string contains.
  • str_rev( ) :
    To reverse a string. It gives output as the reverse of that string. If the string contains more than one word the words will be arranged in reverse order else the letter of the words get reversed.
  • strpos( ) :
    To search for a specific string. It searches for a specific string in the string and returns the fist index of  the string if it is found else -1.
  • str_replace( ) :
    Replace text with in a string. It replaces a specific part of a string with the required word.

A small program to validate all the string manipulation functions in PHP

PROGRAM:

<?php

$r= "i am";

echo("$r");

echo strlen("Hi Every One");

echo str_word_count("Hi Every One");

echo strrev("Hello world");

echo strpos("Hi Buddy", "Buddy");

echo str_replace("world", "jack", "Hello world");

?>

OUTPUT:

i am
12
3
world Hello
3
Hello jack

In the above example, you can see that we used all the string manipulation functions in a single program. So that you can understand  all the functions easily.

You can also read:

Leave a Reply

Your email address will not be published. Required fields are marked *