PHP String Functions Part 2

In Previous post PHP String Functions Part 1 we have explained with examples some PHP string functions, there are many functions to deal with string, I will explain some other functions.

PHP String Functions Examples :


Inserts HTML line breaks before all newlines in a string.

$string = "today's
echo nl2br($string);
/* result : today's 
entries */


Make a string’s first character uppercase

$string = 'hello world';
echo ucfirst($string);
// result : Hello world


Uppercase the first character of each word in a string

$string = 'hello world';
echo ucwords($string);
// result : Hello World


Make a string lowercase

$string = 'HeLLo WoRld';
echo strtolower($string);
// result : hello world


Make a string uppercase

$string = 'hello world';
echo strtoupper($string);
// result : HELLO WORLD


Find the position of the first occurrence of a substring in a string

$string = 'Hello World';
$word = 'llo';
$pos = strpos($string, $word);
if ($pos === false) {
echo "The word was not found in the string";
} else {
echo "The word is found in the string";
// result : The word is found in the string


Strip HTML and PHP tags from a string

$string = '<p>Test paragraph.</p><!-- Comment --> <a href="#fragment">Other text</a>';
echo strip_tags($string);
// result : Test paragraph. Other text
$string = '<p>Test paragraph.</p><!-- Comment --> <a href="#fragment">Other text</a>';
echo strip_tags($string,'<p>'); // allowable HTML
// result : <p>Test paragraph.</p> Other text

Leave a Comment

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