Page 1 of 1

Quick Intro to PHP Strings a short tutorial about php strings Rate Topic: -----

#1 quim  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 18
  • View blog
  • Posts: 182
  • Joined: 11-December 05

Posted 25 May 2008 - 05:57 PM

QUICK INTRO TO PHP STRINGS

PDF format: Attached File  Quick_Into_to_PHP_Strings.pdf (344.39K)
Number of downloads: 405
Title: Quick Intro to PHP Strings
Audience: Beginner ;)
This tutorial is aimed at PHP beginners, or for people that just want to remind about strings in PHP.


Unlike C and Some other languages Strings in PHP are not array of characters. Strings are of type scalar. Type scalar are boolean, integer, double, and string. They can hold only one value at a time.
The three ways to declare/define a string
A string literal can be specified in four different ways:
1. single quoted
2. double quoted
3. heredoc syntax
4. nowdoc syntax (since PHP 5.3.0) “I just learned about this one while I was writing this tutorial”

$text = 'Hello World';
$text = "Oh no!\n Not Hello World again.";
$text = <<<GO
Hello again. This way I can write on and on using “double quote” or ‘single quote’ and as you can see its being treated just as we intended
GO;


STRINGS IN DOUBLE QUOTE ARE INTERPOLATED. THIS MEANS THAT IF YOU PUT A VARIABLE INSIDE IT, IT WILL RETURN THE VALUE OF THAT VARIABLE.
Example:
$text = 'dreamincode';
echo "$text is the best";

WAIT BUT THERE IS A CATCH. IF THE VARIABLE IS AN ARRAY YOU WILL NEED TO HELP PHP WITH THE DECISION.
Example:
$month = array( "April", "May", "June" );
echo "the month is $month[  0 ]";

The code above will not display the value. Instead it will print the string as you see it.
To print the value the way we intended we will have to put the array inside a bracket { } or we can use the dot (.) operator to concatenate strings.
Example:
echo "The month is { $month[ 0 ] }";
echo "The month is " . $month[  1 ];


ALSO, IF YOU USE ESCAPE CHARACTER IN DOUBLE QUOTE STRING IT WILL DO JUST WHAT WE INTEND,
WHICH IS PERFORM ITS TASK LIKE ( \N, \T, \\, \$). BUT IF WE USE ESCAPE CHARACTER IN A SINGLE QUOTE STRING ALL WE GET IS THE TEXT AS IT WAS ASSIGN.


Example: Check how these two identical strings are interpreted differently.
echo "This is a string. \n I just jumped to a new line.";

Output:
This is a string.
I just jumped to a newline.

echo 'This is string. \n I just jumped to a new line.';

Output:
This is string. \n I just jumped to a new line.

STRING AS ARRAY
PHP have the ability to treat strings as Arrays in some situation.
Example:
$text = "Rhode Island";
for( $i = 0; $i < strlen( $text ); $i++ )
{
	echo $text[ $i ] .'<br/>';
}

The example above will loop through the string and print each character.

FUNCTIONS REFERENCE:

nl2br( $text ) --> Converts all newline ( \n or \r ) to <br/>

strlen( $text ) --> return the string length

trim( $text ) --> removes whitespace from both sides
ltrim( $text ) --> removes whitespace from left only
rtrim( $text ) --> removes whitespace from right only

Prints a string. Parentheses are not necessary since they are a constructor of the PHP language.
print( $text )
echo( $text )

str_replace( $search, $replacement, $text ) --> Replace all occurences of the $search string with the $replacement string
str_split( $text ) --> Converts a string to an Array, the second argument $split_length is default to 1
strip_tag( $text ) --> Strip HTML and PHP Tags from string, the second argument $allowable_tags is default to empty

strpos( $text, $search ) --> find the position (index) of the first occurence of $search in $text ($haystack)
strrpos( $text, $search ) --> find the position of the last occurence of the $search string in the $text
strrev( $text ) --> Reverse a string example Hello -> olleH

substr( $text, $start ) --> Return part of a string, the third argument is default to strlen( $text )

strtolower( $text )
strtoupper( $text )

wordwarp( $text, 100 ) --> Wraps a string to a given number of character, the second and third argument are default to 75 and '\n'
by default the work will not be cutoff if you don't play with the fourth argument.


EVEN MORE FUNCTIONS


explode( ' ', $text ) --> returns an array of the string $text, the first parameter is a delimeter( character, comma, anything!? )
implode( ' ', $text_array ) --> it does exactly what explode does, but it differs in that i gets an array and return a string instead.

htmlentities( $text ) returns the text with HTML entities if there is any special HTML character in the text.
Example:
" -> "
& -> &
< -> <
> -> >

rawurlencode( $text ) --> return text in a url format
rawurldecode( $text ) --> return text in a normal format not url


Author: Joaquim Costa aka Quim
Email: quim88@hotmail.com
Please submit any mistakes so I can correct them.




RESOURCES USE FOR THIS TUTORIAL:
PHP. (2008, May 25). PHP: Strings Functions - Manual. Retrieved May 25, 2008, from PHP website: http://us.php.net/strings
PHP. (2008, May 25). PHP: Strings . Retrieved May 25, 2008, from PHP website: http://us.php.net/types.string

This post has been edited by quim: 31 May 2008 - 10:22 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Quick Intro to PHP Strings

#2 chrisman  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • Posts: 46
  • Joined: 22-March 08

Posted 17 June 2008 - 11:39 AM

implode() does the OPPOSITE of explode()

For example:

$date = "2008-06-17";
$array = explode("-", $date);
  // $array[0] == "2008"
  // $array[1] == "06"
  // $array[2] == "17"
$date2 = implode("-", $array);
  // $date2 == $date


This post has been edited by chrisman: 17 June 2008 - 11:39 AM

Was This Post Helpful? 0
  • +
  • -

#3 coolmac112  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 58
  • Joined: 30-August 09

Posted 30 August 2009 - 06:47 AM

does one have to memorise these function or its useful to know some of the capabilities, when you want to do something specific you browse around?

I am in my infant levels of learning programming :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1