PHP, Object-Oriented Programming [Part 3]

PHP, Object-Oriented Programming [Part 3]
Reading time: 1 min read
Link copied!

Greetings!

Continuing from the previous post PHP, Object-Oriented Programming, Part 2, I’ll follow the schedule.

2.3] String Manipulation Functions

strtoupper, The “strtoupper” function serves to transform all characters of a string to uppercase.

<?
	echo strtoupper('today I had an avocado smoothie');
	// Result: TODAY I HAD AN AVOCADO SMOOTHIE
?>

strtolower, The “strtolower” function does exactly the opposite of “strtoupper”, that is, transforms string characters to lowercase.

<?
	echo strtolower('TODAY I HAD AN AVOCADO SMOOTHIE');
	// Result: today i had an avocado smoothie
?>

substr, The “substr” function returns a certain amount of characters from a string.

<?
	$rest = substr("Avocado", 1);
	echo $rest . "\n";
	$rest = substr("Avocado", 1, 3);
	echo $rest . "\n";
	$rest = substr("Avocado", 0, -1);
	echo $rest . "\n";
	$rest = substr("Avocado", -2);
	echo $rest . "\n";
	/*
	Result:
	vocado
	voc
	Avocad
	do
	*/
?>

str_repeat, The “str_repeat” function repeats the string a certain number of times.

<?
	$txt = ".oO0Oo.";
	print str_repeat($txt, 2);
	// Result: .oO0Oo..oO0Oo.
?>

strlen, The “strlen” function returns the number of characters in a string.

<?
	$txt = "The quick brown fox jumps over the lazy dog";
	print 'The length is: ' . strlen($txt) . "\n";
	// Result: The length is: 43
?>

str_replace, The “str_replace” function replaces one string with another.

<?
$txt = "My car is a Civic";
print str_replace('Civic', 'Accord', $txt);
// Result: My car is an Accord
?>

explode, The explode function transforms a string into an Array, separating elements through a separator.

<?
$data = "08/01/2009";
var_dump(explode("/", "$data"));
/*
Result:
array(3) {
   [0]=>
   string(2) "08"
   [1]=>
   string(2) "01"
   [2]=>
   string(4) "2009"
}
*/
?>

implode, The “implode” function transforms an array into string, separating elements through a separator.

<?
$pattern = array('Maria', 'Paulo', 'José');
$result = implode(' + ', $pattern);
var_dump($result);
// Result: string(20) "Maria + Paulo + José"
?>

This was the last post of the PHP introduction, from now on the continuity of this Post will be the Object-Oriented approach.

We’ll be back… []‘s