php strings

Solutions on MaxInterview for php strings by the best coders in the world

showing results for - "php strings"
Jayce
02 Jan 2017
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;
Elena
15 Mar 2019
1
2<?php
3echo 'this is a simple string';
4
5echo 'You can also have embedded newlines in
6strings this way as it is
7okay to do';
8
9// Outputs: Arnold once said: "I'll be back"
10echo 'Arnold once said: "I\'ll be back"';
11
12// Outputs: You deleted C:\*.*?
13echo 'You deleted C:\\*.*?';
14
15// Outputs: You deleted C:\*.*?
16echo 'You deleted C:\*.*?';
17
18// Outputs: This will not expand: \n a newline
19echo 'This will not expand: \n a newline';
20
21// Outputs: Variables do not $expand $either
22echo 'Variables do not $expand $either';
23?>
24
25