-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path08_string_functions.php
47 lines (34 loc) · 1.15 KB
/
08_string_functions.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?php
/* ---------- String Functions -------- */
/*
Functions to work with strings
https://www.php.net/manual/en/ref.strings.php
*/
$string = 'Hello World';
// Get the length of a string
echo strlen($string) . '<br/>'; // 11
// Find the position of the first occurrence of a substring in a string
echo strpos($string, 'o') . '<br/>'; // 4
// Reverse a string
echo strrev($string) . '<br/>'; // dlroW olleH
// Convert all characters to uppercase
echo strtoupper($string) . '<br/>'; // HELLO WORLD
// Convert all characters to lowercase
echo strtolower($string) . '<br/>'; // hello world
// String replace
echo str_replace('World', 'PHP', $string) . '<br/>'; // Hello PHP
// Return portion of a string specified by the offset and length
echo substr($string, 0, 5) . '<br/>'; // Hello
echo substr($string, 6) . '<br/>'; // World
// Starts with
if (str_starts_with($string, 'Hello')) {
echo 'String starts with Hello' . '<br/>';
}
// Ends with
if (str_ends_with($string, 'World')) {
echo 'String ends with World' . '<br/>';
}
$string2 = '<h1>Hello World</h1>';
echo htmlspecialchars($string2);
printf('%s likes to %s', 'John', 'play');
printf('1+1 = %d', 1 + 1);