134 lines
2.8 KiB
PHP
134 lines
2.8 KiB
PHP
<?php
|
|
|
|
/* String Concatenation. */
|
|
|
|
// Using the dot (.) operator
|
|
$string1 = 'Hello';
|
|
$string2 = 'World';
|
|
$concatenation = $string1 . $string2;
|
|
echo $concatenation;
|
|
echo '<br><br>';
|
|
|
|
// Using the concatenation assignment operator (.=)
|
|
$string1 = 'Hello';
|
|
$string2 = 'World';
|
|
$string1 .= ' ' . $string2;
|
|
echo $string1;
|
|
echo '<br><br>';
|
|
|
|
// Using double quotes (") to directly concatenate variables within a string
|
|
$string1 = 'Hello';
|
|
$string2 = 'World';
|
|
$concatenation = "$string1 $string2";
|
|
echo $concatenation;
|
|
echo '<br><br>';
|
|
|
|
/* Newlines */
|
|
|
|
// Using the "\n" escape sequence
|
|
echo "Line 1\nLine 2\nLine 3";
|
|
echo '<br><br>';
|
|
|
|
// Using double quotes (")
|
|
echo "Line 1
|
|
Line 2
|
|
Line 3";
|
|
echo '<br><br>';
|
|
|
|
// Using PHP_EOL constant, which represents the correct end-of-line character
|
|
echo "Line 1" . PHP_EOL . "Line 2" . PHP_EOL . "Line 3";
|
|
echo '<br><br>';
|
|
|
|
// Using the nl2br() function to insert HTML line breaks (\n) before all newlines in a string
|
|
$text = "Line 1\nLine 2\nLine 3";
|
|
echo nl2br($text);
|
|
echo '<br><br>';
|
|
|
|
/* Handling HTML String Input */
|
|
|
|
// htmlspecialchars()
|
|
$string = "<a href='test'>Test & 'Example'</a>";
|
|
$safe_string = htmlspecialchars($string, ENT_QUOTES, 'UTF-8');
|
|
echo $safe_string;
|
|
echo '<br><br>';
|
|
|
|
// htmlentities()
|
|
$string = "<a href='test'>Test & 'Example'</a>";
|
|
$safe_string = htmlentities($string, ENT_QUOTES, 'UTF-8');
|
|
echo $safe_string;
|
|
echo '<br><br>';
|
|
|
|
/* Encoding and Decoding */
|
|
|
|
// Encode
|
|
$url = urlencode("https://fanshawec.ca?programs=iwd&cpa");
|
|
echo $url;
|
|
echo '<br><br>';
|
|
|
|
// Decode
|
|
$url = urldecode("https%3A%2F%2Ffanshawec.ca%3Fprograms%3Diwd%26cpa");
|
|
echo $url;
|
|
echo '<br><br>';
|
|
|
|
/* Adjusting strings */
|
|
|
|
// substr()
|
|
$string = "You are a wizard, Harry.";
|
|
$substr = substr($string, 9, 6);
|
|
echo $substr;
|
|
echo '<br><br>';
|
|
|
|
// strtok()
|
|
$string = "Luke, I am your father.";
|
|
$substr = strtok($string, ",");
|
|
echo $substr;
|
|
echo '<br><br>';
|
|
|
|
// strlen()
|
|
$string = "May the odds be ever in your favour.";
|
|
$length = strlen($string);
|
|
echo $length;
|
|
echo '<br><br>';
|
|
|
|
// Uppercase / Lowercase
|
|
$string = "hello world";
|
|
echo ucfirst($string);
|
|
echo '<br><br>';
|
|
echo ucwords($string);
|
|
echo '<br><br>';
|
|
echo strtoupper($string);
|
|
echo '<br><br>';
|
|
echo strtolower('HELLO WORLD');
|
|
echo '<br><br>';
|
|
|
|
/* Replacing Strings */
|
|
|
|
// str_replace()
|
|
$search = 'world';
|
|
$replace = 'everyone';
|
|
$subject = 'Hello world!';
|
|
echo str_replace($search, $replace, $subject);
|
|
echo '<br><br>';
|
|
|
|
// str_ireplace()
|
|
$search = 'WORLD';
|
|
$replace = 'everyone';
|
|
$subject = 'Hello world!';
|
|
echo str_ireplace($search, $replace, $subject);
|
|
echo '<br><br>';
|
|
|
|
// trim()
|
|
$string = " hello world ";
|
|
echo trim($string);
|
|
echo '<br><br>';
|
|
|
|
// rtrim()
|
|
$string = "hello world!!!";
|
|
echo rtrim($string, '!');
|
|
echo '<br><br>';
|
|
|
|
$string = " hello world";
|
|
echo ltrim($string);
|
|
echo '<br><br>';
|
|
|