PHP Strings Functions

Searching & Replacing String
Write any some logical PHP program you need to search some text for a particular chunk of string. Like, need to know the domain name of user email id or want to know the current page name from page URL.

In PHP language there are some pre-define function to search particular string from any text.

  1. strpos() and strrpos()
  2. strstr()
  3. substr_count()
  4. i. strpos() and strrpos()

    Output

    Explanation
    strpos() function return the position** of any string value from whole text. The first argument is text value and the second argument is string (which you want to find) value. If the string is found then returns the position of this string and if not found it returns -1.
    In above example, we want to search the gmail position from john.smith@gmail.com. It returns 11 the position value of gmail word.
    But when we search yahoo it returns -1 because yahoo is not present in john.smith@gmail.com.
    strrpos() is similar to strpos() but very minor difference.
    strpos() find the string from the starting position and strrpos() find the string from the ending position of text.
    So above example strpos() return the first dot(.) position and strrpos() return last dot(.) position

    Note: ** String position start with zero. Be careful when you using strpos() to search the first string position in any text its returns zero which equivalent to false in PHP (if you check the code in if condition). That’s why use === or !== operator to avoid those problem.

    ii strstr()

    Output

    Explanation

    strstr() function just check the string is present or not in the text. If present returns the string value along with reaming text and if not present return false.
    So in above example first gmail is found so return gmail.com
    And the second yahoo not found so return Not Found.
    Note: strstr() is case sensitive means if you search Gmail it returns false. If you don’t care about the matching case then use stristr() which is case insensitive.

    iii. substr_count()

    Output

    Explanation
    substr_count() function used to find the number of times of a text appears in the target string.
    In above example, o is present two times so it returns 2.

    In your PHP program sometimes you need to replace wrong text from any string. Like spelling check, Wrong spelling replaces to correct spelling. In this case PHP gives you str_replace() function,

    Example

    Output

    Explanation
    str_replace() function used to replace any striing to another string in the chunk of text. The first argument is which striing you want to replace, the second argument is which string replace the previous string and the third argument is where the to replace is actually happening means the text value.
    In above example, you try to replace the wrong striing spelling to correct spelling. So pass the first argument wrong spelling ‘striing’, the second pass the correct spelling ‘string’ and third the text value ‘using string replace function to correct string spelling’.
    After execution, all ‘striing’ word replace to ‘string’.

    Note: str_replace() is case sensitive means if you want to replace String it returns false. If you don’t care about the matching case then use str_ireplace() which is case insensitive.

    Formatting String

    PHP allows to format strings in many different ways to make easy for people to read or passing to another program.
    There are many PHP function to format strings in specific ways.

    1. printf()
    2. sprintf()
    3. fprintf()
    4. vprintf()

    1. printf()

    Output

    Explanation
    In printf() the first argument is a string, it is called format string. It contains regular text or some format specifiers (like in above example %s). format specifiers start with % symbol.
    The second argument is the value which you want to format particular way.
    In above example 123.45 format into co-responding its float, integer and binary integer value style.
    There are some symbol of different format specifier, like %s for string, %f for float, %d for decimal integer, %b for binary integer, %c for ASCII, %e for scientific notation, %x for hexadecimal integer and %o for octal integer.

    2. sprintf()
    sprintf() is similar to prinf() except that rather than directly outputting the result it returns, it so that you can store it in a variable. This is help to process the result before displaying it or store into database also.
    1. printf()

    3. fprintf() use for writing the result to a file.
    iv.4. vprintf() use for work with the array of values to format instead of an argument list.

Leave a Reply

Your email address will not be published.