1$myString = 'Hello Bob how are you?';
2if (strpos($myString, 'Bob') !== false) {
3 echo "My string contains Bob";
4}
1/**
2 * @param string|string[] $plain
3 * @param int $revealStart
4 * @param int $revealEnd
5 * @param string $obscuration
6 * @return string|string[]
7 */
8function obscure(
9 $plain,
10 int $revealStart = 1,
11 int $revealEnd = 0,
12 string $obscuration = '*'
13) {
14 if (is_array($plain)) {
15 return array_map(
16 function ($plainPart) use ($revealStart, $revealEnd, $obscuration) {
17 return obscure($plainPart, $revealStart, $revealEnd, $obscuration);
18 },
19 $plain
20 );
21 }
22 $plain = (string) $plain;
23 return mb_substr($plain, 0, $revealStart)
24 . str_repeat(
25 $obscuration,
26 max(
27 0,
28 mb_strlen($plain) -
29 ($revealStart + $revealEnd)
30 )
31 )
32 . mb_substr(
33 $plain,
34 -$revealEnd,
35 $revealEnd
36 );
37}
1 $output = <<<HTML
2 <p>Lorem ipsum dolor sit amet consectetur<p>
3 <a href="{$foobar}">click here</a>
4HTML;
1#String Functions
2
3substr() #Returns a portion of a string
4===========
5<?php
6 #substr() Returns a portion of a string
7 $output = substr('Hello', 1, 3);
8 $output1 = substr('Hello', -2);//starts from the back of the string
9 echo $output;
10 echo '<br>';
11 echo $output1;
12?>
13===============
14strlen() #Returns the length of a string
15===============
16 $output = strlen('Hello');
17 echo $output;
18?>
19===============
20strpos() #finds the position of the first occurence of a sub string
21===============
22 $output = strpos('Hello World', 'o');
23 echo $output;
24 $output1 = strrpos('Hello World', 'o'); #last occurance
25 echo $output1;
26================
27trim() # trims white space
28================
29 $text = 'Hello World ';
30 var_dump($text);
31 echo '<br>';
32 $trimmed = trim($text);
33 echo $trimmed;
34 echo '<br>';
35 var_dump($trimmed);
36==================
37strtoupper() # makes everything uppercase
38==================
39$text = 'Hello World';
40 $uppercase = strtoupper($text);
41 echo $uppercase;
42==================
43strtolower() #makes everything lowercase
44==================
45 $text = 'Hello World';
46 $lowercase = strtolower($text);
47 echo $lowercase;
48==================
49ucwords() #Capitalizes every word
50===================
51 $text = 'hello world';
52 $proppercase = ucwords($text);
53 echo $proppercase;
54==================
55str_replace() #Replace all occurances of a search string
56 #with a replacement
57==================
58$text = 'hello world';
59 $wordreplace = str_replace('world', 'john', $text);
60 echo $wordreplace;
61=================
62is_string() #Checks to see if it is a string
63=================
64 $val = 'Hello';
65 $output = is_string($val);
66 echo $output;
67 echo '<br>';
68
69 $values = array(true, false, null, 'abc', 33, '33',
70 22.4, '22.4', '', ' ', 0, '0');
71
72 foreach($values as $value){
73 if(is_string($value)){
74 echo "{$value} is a string<br>";
75 }
76}
77=================
78gzcompress() # Compress a string
79=================
80 $string =
81 "a;laksd;lk;lkasd;lkas;lk;lkd;lkasd;lka;lskd;lka;lkd;lk
82 as;l;laksd;lk;lkasd;lkas;ldk;laskd;lakd;lkad;l
83 adslkjlkasjdlkjlkjaslkjaslkdjlkajdlkajdlkajd
84 alskdjlkasjdlkjadlkjadlkjadlkjadlajd
85 adlkjlkjalksjdlkjlkjlkjklajsda";
86
87 $compressed = gzcompress($string);
88
89 echo $compressed;
90 echo '<br>';
91
92 $original = gzuncompress($compressed);
93
94 echo $original;
1<?php
2$str = <<<EOD
3Exemple de chaîne
4sur plusieurs lignes
5en utilisant la syntaxe Heredoc.
6EOD;
7
8/* Exemple plus complexe, avec des variables. */
9class foo
10{
11 var $foo;
12 var $bar;
13
14 function __construct()
15 {
16 $this->foo = 'Foo';
17 $this->bar = array('Bar1', 'Bar2', 'Bar3');
18 }
19}
20
21$foo = new foo();
22$name = 'MyName';
23
24echo <<<EOT
25Mon nom est "$name". J'affiche quelques $foo->foo.
26Maintenant, j'affiche quelques {$foo->bar[1]}.
27Et ceci devrait afficher un 'A' majuscule : \x41
28EOT;
29?>
1
2 <?php
3$x = "Hello world!";
4$y = 'Hello world!';
5
6echo $x;
7echo "<br>";
8echo $y;
9?>