Chapter 2-3 PHP Working With Strings
Chapter 2-3 PHP Working With Strings
PHPWorking
Workingwith
withStrings
Strings(Part
(3 of35)of 5) Slide 1
Slide 1
CHAPTER 2:
PHP Working with Strings (3 of 5)
Topics covered:-
Searching text within a string
Replace some text within a string with another string
Working with Uppercase and Lowercase
String formatting
Regular Expressions
heredoc syntax:-
$myString = <<<DELIMITER
(insert text here)
DELIMITER;
nowdoc syntax:-
The difference between
$myString = <<<‘DELIMITER’
heredoc and nowdoc is with
(insert text here) the single-quote enclosing
DELIMITER; the DELIMITER
heredoc example:-
$religion = ‘Hebrew’;
$myString = <<<DELIMITER
“’I am a $religion,’ he cries – and then – ‘I fear the Lord the God of Heaven
who hath made the sea and the dry land!’”
DELIMITER;
echo “<pre>$myString</pre>”;
Resulted Output:-
"'I am a Hebrew,' he cries - and then - 'I fear the Lord the God of Heaven
who hath made the sea and the dry land!'"
AMIT 2043 Web Systems and Technologies
Refer to code example: stringDemo
PHP
PHPWorking
Workingwith
withStrings
Strings(Part
(3 of35)of 5) Slide 8
nowdoc example:-
$religion = ‘Hebrew’;
$myString = <<<‘DELIMITER’
“’I am a $religion,’ he cries – and then – ‘I fear the Lord the God of Heaven
who hath made the sea and the dry land!’”
DELIMITER;
echo “<pre>$myString</pre>”;
Resulted Output:-
"'I am a $religion,' he cries - and then - 'I fear the Lord the God of Heaven who
hath made the sea and the dry land!'"
• Example 2:-
$studID = '13WAD01234';
if(strlen($studID) != 10){
echo "Invalid student id.";
}else{
echo "Valid student id.";
}
• Noticed that in the above example, negative values are used. If negative
values are supplied, substr() function will count from end of the string
starting from -1, -2 and so on.
• Thus, -10 in the above example means reading start from the last index and
count for 10 characters long.
• strpos() function returns the index of the first character of the found
text. If not found, strpos() return false.
• Syntax:-
strpos(<string to search through>, <search text>)
Note: there is an optional third argument to specify the
index to start the search.
• Example:-
echo strpos(“Hello My Friend”, “My”); //return 6
echo strpos(“Hello My Friend”, “hi”); //return false
echo strpos(“Hello World”, “o”, 5); //return 7
substr_count()
Example: echo substr_count("Hello ld World, my friend, lol", "ld"); //Display 2
• Example:-
echo str_replace("times", "bananas", "It was the best of times, it was
the worst of times“, $occurrence);
echo $occurrence;
Resulted output:-
It was the best of bananas, it was the worst of bananas2
substr_replace() Function
• substr_replace() – replaces a specified portion of the target string with
another string
• Syntax:-
substr_replace(<string>,
<replacement string>,
<start index>)
• With the start index, the replacement string start filling from the index
specified and onwards changing the original string.
substr_replace() Function
• Example:-
$myString = “It was the best times, it was the worst times.”;
echo substr_replace($myString, "bananas", 11);
Resulted Output:-
It was the bananas
Noticed that the rest of the strings are all been replaced with bananas
substr_replace() Function
• If you wish not to replace the rest of the string with the replacement
string and would like to retain them, use the fourth argument to specify
the number of characters to replace.
• Example:-
$myString = “It was the best times, it was the worst times.”;
echo substr_replace($myString, "bananas", 16, 5);
Resulted Output:-
It was the best bananas, it was the worst times.
• Noticed that only the 5 characters “times” starting from index 16 has
been replaced with the replacement string. The rest of the string
remained unchanged.
AMIT 2043 Web Systems and Technologies
PHP
PHPWorking
Workingwith
withStrings
Strings(Part
(3 of35)of 5) Slide 23
substr_replace() Function
• strtr() – replaces certain characters in the target string with other
characters
• Often in web programming, a URL should be made friendly by replacing
all the spaces with ‘+’ (plus) and any apostrophes with ‘-’ (hypen)
symbols.
• Example:-
$urlString = “Here’s is a little string”;
strtr($urlString, “ ‘”, “+-”);
• Resulted Output:-
Here-s+is+a+little+string
Uppercase / Description
Lowercase Function
strtolower() Convert a string to all lowercase.
strstr() stristr()
strpos() stripos()
strrpos() strripos()
• Example:- str_replace() str_ireplace()
$myString = “Hello World”;
if(stristr($myString, “hello”))
echo “Found”;
else
echo “Not Found”; //Display Found
c Treat the argument as an integer and format it as a character with that ASCII value.
f Format the argument as a floating-point number, taking into account the current locale settings (for
example, many European locales use a comma for the decimal point, rather than a period.)
• Example:-
printf("%d", 123); //display 123
printf("%d", -123); //display -123
printf("%-d", -123); //display -123
printf("%+d", 123); //display +123
printf("%+d", -123); //display -123
Note that padding does not truncate the number output, for example,
printf("%06d", 12345678); //display 12345678
However, this is not the case with string padding.
Note : You may put a ( - ) minus symbol in front of the number 20 to make it left-
aligned instead of the default right-aligned.
• Example:-
printf("%'*-50s\n", "Own padding character using (') ");
• Example:-
• Resulted Output:-
Too good t
• Example:-
$welcomeMsg = sprintf(“%.7s”, “Welcome to PHP World”);
echo $welcomeMsg; //display Welcome
• Note: You may specify the second argument for the type of character to
trim.
• Resulted Output:-
&&&&Benjamin Franklin&&&&&
• This function allows you to format the number with thousand separator
(,) and the specified number of decimal places.
• Of course, if you wish to use a different symbol for the thousand
separator, say (<space>), you can specify it into the number_format()
function as a second argument.
• Syntax:-
number_format(12345678, 2, “!”, “ ”);
• Example:- Number of decimal point Symbol for decimal
echo number_format(1234567823, 1, “!”, “@”);
Symbol for thousand
separator
• Resulted Output:-
1@234@567@823!0
Meta-characters Slide 44
Meta-character Description
\ Escape character
^ beginning of a string
$ the end of a string
. Any single character except newline
| Alternatives (or)
[ Start of a character class
] End of a character class
( Start of a subpattern
) End of a subpattern
{ Start of a quantifier
} End of a quantifier
Quantifiers Slide 45
Character Meaning
? 0 or 1
* 0 or more
+ 1 or more
{x} Exactly x occurrences
{x, y} Between x and y (inclusive)
{x,} At least x occurrences
regular_expr.php
AMIT 2043 Web Systems and Technologies
PHP
PHPWorking
Workingwith
withStrings
Strings(Part
(3 of35)of 5) Slide 47