Sie sind auf Seite 1von 16

F5224 WEB PROGRAMMING

STRING IN PHP ENVIRONMENT


Prepared by: Munirah Abd

PHP String IntroductionThe string functions allow you to manipulate strings. A string variable is used to store and manipulate text.

strlen() function
DEFINITION & USAGE
The strlen() function returns the length of a string.

SYNTAX strlen(string)

strlen() function
Parameter string Description Required. Specifies the string to check

strlen() function
EXAMPLE
<?php echo strlen("Hello world!"); ?> OUTPUT

12

strpos() function
DEFINITION & USAGE
The strpos() function finds the position of the last occurrence of a string inside another string. This function returns the position on success, otherwise it returns FALSE.

SYNTAX
strpos(string,find,start)

strpos() function
Parameter string Description Required. Specifies the string to search

find start

Required. Specifies the string to find Optional. Specifies where to begin the search

strpos() function
EXAMPLE
<?php echo strpos("Hello world!","wo"); ?> OUTPUT

strstr() function
DEFINITION & USAGE
The strstr() function searches for the first occurrence of a string inside another string. This function returns the rest of the string (from the matching point), or FALSE, if the string to search for is not found.

SYNTAX
strstr(string,search)

strstr() function
Parameter Description

string

Required. Specifies the string to search Required. Specifies the string to search for. If this parameter is a number, it will search for the character matching the ASCII value of the number

search

strstr() function
EXAMPLE 1
<?php echo strstr("Hello world!","world"); ?> OUTPUT

world!

substr() function
DEFINITION & USAGE

The substr() function returns a part of a string.


SYNTAX
substr(string,start,length)

substr() function
Parameter string start Description Required. Specifies the string to return a part of Required. Specifies where to start in the stringA positive number - Start at a specified position in the string A negative number - Start at a specified position from the end of the string 0 - Start at the first character in string Optional. Specifies the length of the returned string. Default is to the end of the string.A positive number - The length to be returned from the start parameter Negative number - The length to be returned from the end of the string

length

substr() function
EXAMPLE 1
<?php echo substr("Hello world!",6); ?> OUTPUT

world!

substr() function
EXAMPLE 2
<?php echo substr("Hello world!",6,5); ?> OUTPUT

world

THE END

Das könnte Ihnen auch gefallen