Follow me on Twitter to receive updates, free scripts and ongoing announcements!

PHP string manipulation 2

Written by Codes Tips on July 31, 2009 – 2:34 pm -

This is the second part of the how you can manipulate string in PHP.

Find length of a string

If you want to find out how many characters have a string you can use the function strlen. If you have to pass 1 argument as the string you wish you find out the length and it returns an integer.
Declaration: int strlen(string text)
Example:

Output: 31

Find a sub string inside a string

To find a first occurrence of a string inside other you should use the function strstr and pass it 2 arguments. First argument should be the string in which you wanna look and the second should be the string you search. It returns part of the first occurrence until the end of the string you search in.
Declaration: string strstr(string searchIn,string lookfor)
Example:

Output: strstr Function Test

Find a string inside other string(case insensitive)

To find an occurrence of a string inside another with case insensitive activated you should use the function stristr. It has the same parameters as the strstr, but it also finds case insensitive strings.
Declaration: string stristr(string SearchIn, string lookFor)
Example:

Output: StrStr Function Test

Find position of first occurrence of a string inside another

To find the position of the first occurrence of a string you can use the function strpos. You should pass two arguments. The first parameter is the string you wanna search in and the second is a the string you are looking for. You can specify an additional argument, an offset that indicates from where position the search should begin.
Declaration: int strpos ( string $SearchIn, mixed $SearchFor[, int $offset= 0 ] )
Example:

Output: 11

Find a position of a string inside another(case insensitive)

You can use the function strripos and it has the same arguments as strpos, but it searches for case insensitives.
Declaration: int strripos ( string $SearchIn, mixed $SearchFor[, int $offset= 0 ] )

How to return parts of a string

To return parts of a string you should use the substr function and pass it 2 arguments and 1 optional. The first argument is the string you wanna return a part of a string, second is the start position from where you wanna begin crop the string. You can also specify an argument with the length that the function should crop.
Declaration: string substr ( string $SearchIn , int $from [,int $length])
Example:

Output: is a substr

Insert after each end of line in string a html br tag

To insert html tag br after the new lines in a string you can use the function nl2br. Function takes one neccessary argument and one optional. First is a string where it will be inserted the br tag and the second one is a boolean that indicates if the replacement is a XHTML compatible line breaks(default value is TRUE).
Declaration: string nl2br(string $InsertIn [,bool $isXHTML= true])
Example:

Output:
This is a nl2br<br /> Function<br /> Test

PHP string manipulation - part 1 - PHP string manipulation - part 3


Tags: ,
Posted in Codes, PHP/MYSQL |

2 Comments to “PHP string manipulation 2”

  1. PHP string manipulation | Codes Tips Says:

    [...] PHP string manipulation - part 2 Bookmark It Posted in Codes, PHP/MYSQL | [...]

  2. PHP string manipulation | Codes Tips Says:

    [...] PHP string manipulation - part 2 Bookmark It Tags: PHP, PHP/MYSQLPosted in Codes, PHP/MYSQL | [...]

Leave a Comment

You must be logged in to post a comment.