PHP string functions and regular expressions

Written by Tully on July 1, 2009 Categories: PHP Tags: , , ,

PHP String Functions & Regular
Expressions

 

String Functions

 

Function

What it does

Example

Concatenation

Add’s strings together.

$x . $t

     Addslashes($str)

Returns a string with backslashes before characters that
need to be quoted in database queries etc. These characters are single quote
(), double quote (), backslash () and NUL (the NULL byte).

string addslashes
( string $str )

 

Rtrim($x)

rtrimStrip
whitespace (or other characters) from the end of a string

string rtrim
( string $str [, string $charlist
] )

 

Ltrim($x)

Strip whitespace (or other characters) from the
beginning of a string.

string ltrim
( string $str [, string $charlist
] )

 

Strlen($x)

Returns the length of the given string .

int strlen
( string $string )

 

Strpos($str,$x)

 

Find position of first occurrence of
a string

 

int strpos
( string $haystack , mixed $needle
[, int $offset= 0
] )

 

Explode($d,$str)

explodeSplit a string by string

 

array explode
( string $delimiter , string $string
[, int $limit ] )

 

implode($d,$str)

Join array elements with a string.

string implode
( string $glue , array $pieces
)

 

Soundex($str)

Calculate the soundex key of a
string.

string soundex
( string $str )

Metaphone($str)

Calculate the metaphone key of a
string

string metaphone
( string $str [, int $phones= 0 ] )

 

 

 

Regular Functions

 

Function

Description

Example

Ereg($p,$str,[$m])

Regular expression match

int ereg
( string $pattern , string $string
[, array &$regs ] )

int eregi
( string $pattern , string $string
[, array &$regs ] )

 

Eregi($p,$str, [$m])

Case insensitive regular expression
match

int eregi
( string $pattern , string $string
[, array &$regs ] )

 

Ereg_replace($p,$r,$str)

Replace regular expression

string ereg_replace
( string $pattern , string $replacement
, string $string )

 

Sql_regcase($p)

Make regular expression for case
insensitive match

string sql_regcase
( string $string )

 

Split($p,$str,[$limit])

Split string into array by regular
expression

array split
( string $pattern , string $string
[, int $limit ] )

 

Preg_match()

Perform a regular expression match

int preg_match
( string $pattern , string $subject
[, array &$matches [, int $flags
[, int $offset ]]] )

 

Preg_match_all()

Perform a global regular expression
match

int preg_match_all
( string $pattern , string $subject
, array &$matches [, int $flags
[, int $offset ]] )

 

Preg_replace()

Perform a regular expression search
and replace

mixed
preg_replace ( mixed $pattern
, mixed $replacement
, mixed $subject
[, int $limit= -1
[, int &$count ]] )

 

Preg_split()

Split string by a regular expression

array preg_split
( string $pattern , string $subject
[, int $limit= -1
[, int $flags= 0
]] )

 

 

 

MetaCharacters

.  

 

Any ONE Character

[A-Z0-9]

 

Any one of the chars

[^a-cx-z]

Not one of the chars

^

Begins with

$

End with

X{2}

2 consecutive “x”

X{2,5}

Between 2 and 5 consecutive “x”

X?

0 or 1 “x”

X+

1 or more “x”

d

Digit

D

Non-digit

w

Word

W

Non-word

s

Space char

S

Not a space char

(x|z)

X or Z

 

No Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>