Home » PHP » PHP String – what is sting in php5?

PHP String – what is sting in php5?

PHP String

A php string is a sequence of characters, like “Welcome to My Site”.

String Functions in php:-

In this lecture we will look at some commonly used functions to Define strings.

Length of a String:-

The PHP strlen() function returns the length of a string.
The example below returns the length of the string “Welcome to My Site”:-
echo strlen(“Welcome to My Site”); // outputs 18?>
The output of the code above will be: 18.

Count The Number of Words in a String:-

The PHP str_word_count() function counts the number of words in a string:-
echo str_word_count(“Welcome to My Site””); // outputs 4?>

Reverse a String:-

The PHP strrev() function reverses a string:-
echo strrev(“Hello world!”); // outputs !dlrow olleH?>
out put is !dlrow olleH

Search For a Specific Text Within a String:-

The PHP strpos() function searches for a specific text within a string.If a match is found, the function returns the character position of the first match. If no match is found, it will return FALSE.
The example below searches for the text “world” in the string “Hello world!”:-
echo strpos(“Hello world!”, “world”); // outputs 6?>
The first character position in a string is 0 not 1.

Replace Text Within a String:-

The PHP str_replace() function replaces some characters with some other characters in a string.
The example below replaces the text “world” with “s1school”:-
echo str_replace(“world”, “s1school”, “Hello world!”); // outputs Hello s1school!?>
You Can Also Visit: PHP Constants
, ,

Leave a Reply

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


error: Sorry You Have Not Permission To Copy!