String Functions in php with example

While developing php web application, often we received user input data as string, before inserting data into database table, we need to make sure data is correct and validated properly, that’s where all these string validation functions are required.

In this tutorial you will learn Php string Functions like substring, replace, converting string to uppercase, lowercase, getting string length count, string etc.

Php built-in string function

Some basic PHP String functions with examples that every Beginner should learn

Let's learn how to deal with string in Php, you will learn with following string function examples.

  • string word count str_word_count()

    This built-in function can count total number of words in any given string

    echo str_word_count("We are learning PHP string function at WebTrainingRoom!");
    // outputs 8
  • string reverse method strrev()

    This built-in function can reverse a string, see the example below.

    echo strrev("I Love You");
    // output would be "uoY evoL I"
  • string position strpos(big_string, small_string)

    Use this function when you want to search a small string within a big string

    echo strpos("We are learning PHP string function at WebTrainingRoom!", "PHP");  
    // output would be 16

    Note: this string search is case sensitive, for example in same string if you search for “php” instead of “PHP”, it will not find the string, so will not give any result.

  • string replace method str_replace()

    Use this function when you want to replace a small string within a big string

    str_replace("Find", "replace with", "big string");
    echo str_replace("We", "You", "We are the best!"); 
    // output would be "You are the best!"
  • String to lower strtolower() and String to upper strtoupper()

    You can convert any string into uppercase and lowercase using built-in functions

    // to lowercase 
    echo strtolower("This is my WORLD.");
    // to uppercase 
    echo strtoupper("This is my WORLD.");
  • Substring method substr(string,start,length)

    this function returns the extracted part of any string, or an empty string, or FALSE on failure

    echo substr("How are you doing!",6)
  • trim function trim()

    trim() Remove any spaces from both sides of a string, This function is very useful when you want any space to be removed from user input, situation like user name and password, we want to remove all whitespace from both side before saving or comparing values.

    echo trim("  I love my country      ");

    Now you may come across situation when you want to remove whitespace value from just one side, either left or right side, but not from both side, in such situation you can use following functions.

    • ltrim() - ltrim removes whitespace from left side
    • rtrim() - rtrim removes whitespace from right side

You may be interested in reading following posts:

Php String function
Learn php programming, php web development with free tutorials
Other Popular Tutorials
String methods in Php
PHP Examples | Join PHP Online Course