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 »

PHP string manipulation

Written by Codes Tips on July 28, 2009 – 2:53 pm -

This is a first part of PHP string manipulation.
It’s extremelly useful to know how to manipulate strings in PHP. This tutorial will guide you through how to use the functions that PHP offers.
A string in PHP it’s defined in php in a variable assigning a single or double quote value like:
$string = ‘This is a test string’;
$string = “This is a test string”;

Make all letters from string lowercase

To make every letter of a string in lowercase you should use the function strtolower like this:

<?
$string= "This is a  strtolower Function Test";
echo strtolower($string);
?>

The ouput will be: this is a strtolower function test

Make all letters of a string uppercase

You should use the function strtoupper in order to make every letter of the string in uppercase:

<?
$string= "This is a  strtoupper Function Test";
echo strtoupper($string);
?>

The ouput of will be: THIS IS A STRTOUPPER FUNCTION TEST

Capitalize first letter of each word in a sentence with ucwords

To make a sentence more estetic we can capitalize first letter of each word in a sentence with the function ucwords().

<?
$string= "This is a  ucwords Function Test";
echo ucwords($string);
?> 
Output: This Is A Ucwords Function Test

This function touch only the first letter of each word and leave other characters like they are. We can improve the way how the string is displayed by combining with the strtolower function.

<?
$string= "ThiS is a  ucwords FunctioN Test";
echo ucwords(strtolower($string));
?>

Output will be:This Is A Ucwords Function Test

Remove tabs,spaces,new lines from beginning and end of a string

To remove spaces,tabs and new lines from the beginning and end of a string you should use the function trim(). This function accepts as an input a string, that will be cleaned and returns a string without spaces,tabs and new lines.

<?
$string= "\t \nThis is a  trim Function Test   ";
echo trim($string);
?>

Output: This is a trim Function Test

Remove spaces,tabs and new lines from begining of a string

Similar like trim we can use ltrim to remove spaces,tabs and new lines from the beginning of a string

<?php
$string= "\t \nThis is a  trim Function Test \t\t  ";
echo ltrim($string);
?>

Output: This is a trim Function Test

Remove spaces,tabs and new lines from the end of a string

Similar like ltrim we can use rtrim to remove characters that we don’t need:

<?
$string= "\t \nThis is a  rtrim Function Test \t\t  ";
echo rtrim($string);
?>

Remove tags from a string

To remove tags from a string you should use strip_tags to have a clean string without tags

<?php
$string= "<pre>This is a  trim Function Test</ pre>";
echo strip_tags($string);
?>

PHP string manipulation - part 2


Tags: ,
Posted in Codes, PHP/MYSQL | 1 Comment »

Plugintaylor.com - Plugintaylor and Weblog Tools Collection