PHP String Handling

Trimming:      trim(), ltrim(), rtrim().
Character Case Management :  ucfirst(), toupper(), tolower().
String Modification: strip_tags(), addslashes(), stripslashes(), htmlentities(), html_entity_decode(), str_shuffle(), md5(),
String Content Searching : strstr(), arrary_search(), strpos(), str_replace(), stristr().
 list(), explode,implode,strlen

$title = basename($_SERVER['SCRIPT_FILENAME'], '.php');
$title = str_replace('_', ' ', $title);//contact_us.php
$title = ucwords($title);//Contact

<?php
$summary = <<< summary
In the latest installment of the ongoing Developer.com PHP series,
I discuss the many improvements and additions to
<a href="http://www.php.net">PHP 5's</a> object-oriented architecture.
summary;
$words = sizeof(explode(' ',strip_tags($summary)));
echo "Total words in summary: $words";
?>


<?php
$url = "sales@example.com";
echo ltrim(strstr($url, "@"),"@");
?>
//example.com


<?php
// Limit $summary to how many characters?
$limit = 100;

$summary = <<< summary
In the latest installment of the ongoing Developer.com PHP series,
I discuss the many improvements and additions to
<a href="http://www.php.net">PHP 5's</a> object-oriented
architecture.
summary;

if (strlen($summary) > $limit)
$summary = substr($summary, 0, $limit).'...';
echo $summary;

?>


<?php
$password = "3312345";
if (strspn($password, "1234567890") == strlen($password))
echo "The password cannot consist solely of numbers!";
?>


<?php
$password = "a12345";
if (strcspn($password, "1234567890") == 0) {
echo "Password cannot consist solely of numbers!";
}
?>
Using   herdoc
$fish = 'whiting';
$mockTurtle = "\"Will you walk a little faster?\" said a $fish to a snail.
\"There's a porpoise close behind us, and he's treading on my tail.\""
echo $mockTurtle;


solution is

$fish = 'whiting';
$mockTurtle = <<< Gryphon
"Will you walk a little faster?" said a $fish to a snail.
"There's a porpoise close behind us, and he's treading on my tail."
Gryphon;
echo $mockTurtle;


String Matching
$string = "The quick brown fox jumps over the lazy dog";
echo preg_match('/fox/', $string) ; // returns 1
Here are the promised examples:
echo preg_match('/^fox/', $string) ; // returns 0
echo preg_match('/^The/', $string) ; // returns 1
echo preg_match('/^the/', $string) ; // returns 0
echo preg_match('/dog$/', $string) ; // returns 1
echo preg_match('/f.x/', $string) ; // returns 1


Our final example is more complex: we will try to verify a North American phone
number (with area code) in the pattern of 999-999-9999. We are looking for a first
character in the range of 2 to 9 (North American area codes don’t start with a 1); the
next two characters in the range of 0 to 9; a hyphen followed by three digits, all in the
range of 0 to 9; and another hyphen followed by four digits in the range of 0 to 9. It
sounds complicated, and it is something that a basic string function just can’t do efficiently
(if at all).


$phone_num = "903-543-5454";
$pattern = "/^[2-9]{1}[0-9]{2}-[0-9]{3}-[0-9]{4}$/" ;
echo preg_match($pattern, $phone_num) ; // returns true

String Substituting


$string = "The quick brown fox jumps over the lazy dog" ;
echo preg_replace('/fox/', 'cheetah', $string) ;
The browser output is:
The quick brown cheetah jumps over the lazy dog

$look_for = array("/4/", "/-/");
$replace_with = array('7', '*');
echo preg_replace($look_for, $replace_with, $phone_num) ;
The following output is produced:
903*573*5757

String Splitting


$pattern = "#[+*/-]#" ;
$formula = "36+15/5*12" ;
$operands = preg_split($pattern, $formula) ;
var_dump($operands) ;
The output is:
array(4) { [0]=> string(2) “36” [1]=> string(2) “15” [2]=> string(1) “5” [3]=> string(2)
“12” }

Reactions:

0 comments:

Post a Comment