Archive for August, 2009
PHP string manipulation 3
Written by Codes Tips on August 1, 2009 – 1:00 pm -This is the third part about how to manipulate strings in PHP.
Replace all occurrences of sub strings with a replacement string
If you wanna replace all occurrence of a substring inside a string you should use the function str_replace passing as first argument the string(or array of strings) you wanna search, second argument the string(or array of strings) you wanna make the replace, the third is the string you wanna search in and the fourth argument is the number of replacements the function should make. It returns a string after all replacements have been made.
Declaration: mixed str_replace ( mixed $SearchFor, mixed $ReplaceWith , mixed $String[, int &$number] )
Example:
<?php $string= "This is a str_replace Function Test"; $searchFor = array("Function","Test"); $replaceWith = array("function","test"); echo str_replace($searchFor,$replaceWith,$string); ?>
Output: This is a str_replace function test
Split string one by one
To tokenize string in php you can you the function strtok in order to split a string. Each smaller string it’s separated by a delimeter. It takes as a first argument the string that will be split and the second the delimeter that it is used to split it.
You should call the first time the function as strtok($String,$delimiter) and returns the first string that is delimited. Any subsequent calls of the function should be like strto($delimiter(s)), because the function keeps into memory the variable $String that it’s delimited.
You can specify multiple delimiter
Declaration: string strtok(string $String, string $delimiter(s))
Example:
<? $string = "This is an strtok Function Test"; $tok = strtok($string, " "); while ($tok !== false) { echo "String=$tok<br />"; $tok = strtok(" ");//returns the next splitted string in the $string variable } ?>
Output:
String=This
String=is
String=an
String=strtok
String=Function
String=Test
Split a string by a delimeter
Disadvantage of strtok function it’s that you should call it every time you wanna split the string. You can use explode function in order to split the string and returns an array of string with all the string splitted. The first argument is the delimiter, the second the string you wanna split and you can also specify an optional integer argument that specify the limit of strings splitted.
Declaration: array xplode(string $delimiter , string $String[, int $limit ])
Example:
<? $string = "This is an explode Function Test"; $explodedstring = explode(" ", $string,3); foreach($explodedstring as $substring) echo $substring.'<br />'; ?>
Output:
This
is
an explode Function Test
Union elements of array in a string
If you wanna join array elements into 1 string you should call the function implode. The first argument that you should pass to the function is the separation and the second one the array of string.
Declaration: string implode ( string $separator , array $Strings)
Example:
<? $strings = array('This','is','a', 'implode','function','test'); $separator = " "; echo implode($separator,$strings); ?>
Output:
This is a implode function test
Replace a string by regular expression
You can use regular expression in php to replace using the function preg_replace. You should pass the first argument as a regular expression pattern - it can be a string or an array of string, the second is the string or array of string that you use to replace the pattern, third argument is the string you search in. The final two arguments are optional, first parameter is the limit of replacement you should make and the final one is the value of replacements that have been made in the string.
Declaration: mixed preg_replace (mixed $regularExpression, mixed $replaceWith, mixed $String [, int $limit= -1 [, int &$count ]] )
Example:
<? $string = "This is a preg_replace bad, words, Function Test"; $pattern = "(\w+,)"; $replaceWith = ""; echo preg_replace($pattern, $replaceWith, $string)."<br />"; ?>
Output:
This is a preg_replace Function Test
Tags: PHP, PHP/MYSQL
Posted in Codes, PHP/MYSQL | 1 Comment »

















