Star Hoster
Photoshop Tutorials
Anonymous Web Surfing
Funny Jokes
Myspace Friends
Open Treasure Chest
Building in Paradise


July 13, 2008

Strings in Php

Filed under: Intro PHP Tutorials — phpdeveloper @ 1:05 pm

Strings in PHP

After integer.. maybe just after integer, strings are possibly the most important variable types in programming. Storing a bunch (series) of character as one variable. PHP give you alot of functions to manipulate and handle strings.

You can think of string as an array of characters, and individuals characters can be accessed using the index of the character withing the string.

< ?php

$my_string = 'My First String in Php';
$our_string = "My Second String in Php";

$h = "Hello";
$w = "World";
$hw = $h . $w; // appending Hello and World
$h .= $w; // Hello World appending world to hello

$first_letter = $hw[0];
$last_letter = $hw[strlen($hw)-1];

?>

In printing the last character in my string the function strlen was used. Strlen, return the lenght of the string as an integer. So we used the length of the string minus 1, since we are using arrays that start at 0, to get the last character of the string.

Next very useful function is chr, which takes the ascii value and translate it into a string. You can use chr to generate very good passwords

< ?php
$password_len = 5;
$password = "";
$inex = 0;
while($index <= $password_len)
{
$password .= chr(rand(33,126));
$index++;
}
echo $password;
?>

You can check the ASCII-table at http://www.asciitable.com

More string manipulation

Getting Rid of White Space or other Characters

string rtrim ( string str [, string charlist] )
string ltrim ( string str [, string charlist] )
string trim ( string str [, string charlist] )

[, string charlist] optional paramater

rtrim — Strip from the end of a string
ltrim — Strip from the beginning of a string
trim — Strip from the beginning and end of a string

These function returns a string with whitespace stripped from the beginning and end of str. Without the second parameter these functions will strip these characters

” ” (ASCII 32 ), an ordinary space.
“\t” (ASCII 9 ), a tab.
“\n” (ASCII 10), a new line (line feed).
“\r” (ASCII 13), a carriage return.
“\0″ (ASCII 0 ), the NUL-byte.
“\x0B” (ASCII 11 ), a vertical tab.

Sub-String, String-Replace, Change Case

Changing Case

strtoupper() // Changes a string to Upper Case
strtolower() // Changes a string to lower case

These to functions speak for themselves (very good function naming)

ucwords — Uppercase the first character of each word in a string
The definition of a word is any string of characters that is immediately after a whitespace (These are: space, form-feed, newline, carriage return, horizontal tab, and vertical tab).

ucfirst — Make a string’s first character uppercase

Sub-String

Substring (Substr), return the string, starting at the position defined by the start and either to the end of the string or the number of characters defined in the third parameter.

string substr ( string string, int start [, int length] )
[,int length] optional paramater

$orig_string = “phptutorialsite.com” ;

$first_part = substr($orig_string,0,3); // php
$last_part = substr($orig_string,3,16) ; //tutorialsite.com

Strstr & Stristr — Find first occurrence of a string

Strstr and Stristr (case-insensitive searches) returns the part of the string from the first occurrence of the string being searched for until the end of the string. If the string is not found the function returns FALSE (boolean).

string strstr (string where_to_look, string what_to_find)
$orig_string = “phptutorialsite.com”;
$php_tutorial = stristr($orig_string, “php”); //tutorialsite.com

$what_to_find = “PHP”;
$where_to_look = “php Tutorial Site “;

if(strstr($where_to_look,$what_to_find))
{
echo “Yes I found Php Tutorial in this string on this php tutorial site.”;
}
else
{
echo “Php not in this string.”;

}

StrPos - Find position of first occurrence of a string
int strpos ( string where_to_find, mixed what_to_find [, int offset] )

This works just like strstr, however this return the position of the search string within main string. If the string is not found strpos returns FALSE (boolean).

Implode and Explode
The last 2 functions we are going to cover in this tutorial are explode () and implode (). These functions are used in converting strings to arrays and arrays to strings.

Explode takes the elements in a string “seperated” by delimiter and places those elements into an array.

e.g It can take a date 07/08/2006 and put it into an array date, where date[0] = 07, date[1]=08 and date[2]=2006, where “/” is the delimiter.

eg.
string siteurl = “www.phptutorialsite.com”;

$site=explode(”.”, $siteurl);

site[0]=> www
site[1]=> phptutorialsite
site[2]=> com

Implode takes the element of an array, and put them into a string, with the “separator” being the first parameter specified in the implode functions.

e.g

sitename[0]=php
sitename[1]=good
sitename[2]=tutorial
sitename[3]=site
sitename[4]=best

$sitefullname= implode (” “, $sitename);
echo $sitefullname; //php good tutorial site best

Syntax :
explode() => array explode ( string separator, string string [, int limit] )
implode() => string implode ( string glue, array pieces )

Closing
These are just a few of the functions, that can be used to manipulate strings in php, for a full list of string function, you can visit. http://www.php.net/manual/en/ref.strings.php .

No Comments

No comments yet.

RSS feed for comments on this post.

Sorry, the comment form is closed at this time.