PHP TUTORIALPHP TutorialPHP InstallationPHP Hello WorldPHP Basic SyntaxPHP CommentsPHP VariablesPHP Variable ScopePHP ConstantsPHP StringsPHP OutputPHP Data TypesPHP Type CastingPHP OperatorsPHP ConditionalsPHP Shorthand ConditionalsPHP LoopsPHP Loop Control StructuresPHP FunctionsPHP String FunctionsPHP ArraysPHP Superglobal VariablesPHP in HTMLPHP AdvancedPHP Include and RequirePHP RegexRegex IntroductionRegex PCRE SyntaxPHP PREG FunctionsPHP FormsPHP Forms IntroductionPHP Forms CreatingPHP Forms SecurityPHP Forms ValidationPHP Forms Required InputsPHP Forms StickyPHP Forms Advanced ValidationPHP Forms FinishingPHP OOPPHP OOP IntroductionPHP OOP ClassesPHP OOP PropertiesPHP OOP ObjectsPHP OOP MethodsPHP OOP $this KeywordPHP OOP Constructors and DestructorsPHP OOP VisibilityPHP OOP InheritancePHP OOP Abstract Classes and MethodsPHP OOP InterfacesPHP OOP TraitsPHP OOP ConstantsPHP OOP StaticPHP OOP Namespaces

PHP String Functions

PHP Strings Functions

PHP has a lot of string functions. Here we will learn about commonly used string functions with its usage.

Getting the Length

The strlen() function can be used to get the length of a string.


<?php
$str = 'Hyvor Developer';
echo strlen($str); // outputs 15

Run Example ››

Number of Words

The str_word_count() function returns the number of words in a string.


<?php
$str = 'This is a string with seven words';
echo str_word_count($str);

Run Example ››

Changing The Case

The strtolower() and strtoupper() are used to make a string lowercase and uppercase respectively.


<?php
$str = 'Hyvor Developer';

echo strtolower($str); // hyvor developer
echo '<br>';
echo strtoupper($str); // HYVOR DEVELOPER

Run Example ››

Removing Unnecessary Whitespace

The trim() function removes unnecessary whitespace from the beginning and the end of a string.


<?php
$str = '  Hyvor Developer    ';
echo strlen($str) . '<br>'; // length is 21

$str = trim($str); // remove whitespace
echo strlen($str); // now, length is 15

Run Example ››

Note: The trim() function is commonly used with PHP Forms.

Reverse

The strrev() function is used to reverse a string.


<?php
$str = 'Hyvor Developer';
echo strrev($str);

Run Example ››

The strpos() function returns the position of the first occurrence of a substring in a string.


<?php
$str = 'Hello World';
echo strpos($str, 'World'); // will output 6

Run Example ››

Tip: In PHP, index of strings starts from 0. Therefore, the strpos() function returns 6 instead of 7 in the above example.

Replacing Text within a String

The str_replace() function replaces a text within a string.

In this example, "Morning" in "Good Morning" will be changed to "Evening". The final result will be "Good Evening".


<?php
$str = 'Good Morning';
echo str_replace('Morning', 'Evening', $str);

Run Example ››

Repeating a String

The str_repeat() function is used to repeat a string number of times.


<?php
echo str_repeat('*', 20);

Run Example ››

Formatted Strings

The sprintf() function is used to get formatted (or dynamic) strings.


<?php
$amount = 5.44;
echo sprintf('The amount is $%F <br>', $amount);
$myName = 'Hyvor';
$age = 10;
echo sprintf("My name is %s. I'm %d years old", $myName, $age);

Run Example ››

% is used as a placeholder. And, you can have any number of arguemnts.

  • %d - Decimal
  • %s - String
  • %f - Float
Facebook Twitter