TUTSCODE PHP 005: PHP STRING HANDLING


Posted on 2015-07-08 07:58:50


String is a very important data type in PHP, some data types such as integer or float in many cases can be considered as the string. Here I will give some common functions for string manipulation. 1. Group functions to the screen in sequence Among the functions in the screen sequence we launched two basic functions commonly used are: echo and print. Using:
1
2
3
4
5
6
7
echo "Hello world";
echo ("Hello world");
print "Hello world";
print("Hello world");
Although in the example above, the results in the display is the same but the two echo and print functions also have a different point is: echo function  return value no longer print function always returns a value of 1. So I recommend that you use the echo function, it will be faster than the print. 2. The function strlen ($ string) Strlen function works total character count of one chain. For example:
1
2
3
4
5
$x = "hello";
$ Y = strlen ( $ x );
Echo $ y ;
The result of this PHP will print to the screen the value of $ y = 5; because the length of the string $ x = "hello" is 5. 3. Function str_word_count($string) This function works count words in a string. For example:
1
echo str_word_count("toi yeu em");
Results: 3 4. The function addslashes ($ string) This function works more character "\" in front of the characters like 'or' in string $ string with effect avoid sql injection error when adding data to the database. For example:
1
echo addslashes ( "My life" alone "should go wherever it alone ' );
Print results: My life \ "alone \" should go wherever it alone 5. The function stripslashes ($ string) This function works in reverse with addslashes function as it removes all characters \. The purpose of this function is handled in the chain before the browser. 6. Function str_replace($search, $replace, $subject) Str_replace function effective search and replace a substring with another string. For example:
1
2
3
<? Php
echo str_replace("world","Blanka","Hello world!");
?>
Print results screen: Hell0 Blanka! Str_replace search function string "world" in the string "Hello world!" And replaced by the string "Blanka" if found. Add a very useful example of str_replace function:
1
2
3
4
5
$str 'Hello! codomotoxo@ yamaha#bonjua% sed$ hehe';
$str str_replacearray('!''@''#''$''%') , ''$str );
echo $str;
Result: Hello codomotoxo yamahabonjua sed hehe In the above example the function str_replace replaces all the characters:!, @, #, $,% With an empty string. " 7. Group functions between arrays and string manipulations * Hàm explode($sub, $string) This function works into 1 cup a string array in 1 cup chain. For example:
1
2
3
4
<? Php
$str = "Hello everybody. My name is Blanka.";
print_r (explode(" ",$str));
?>
Result : Array ( [0] => Hello [1] => everrybody. [2] => My [3] => name [4] => is [5] => Blanka. ) Function explode separated into 1 string $ str element array 6 of 6 from separated by a space. * Function implode($sub, $array) Implode have the opposite effect to explode function, ie it will connect the array element of the array $ 1 $ strings connected via sub chains. For example:
1
2
3
4
<? Php
$arr = array('Hello','World!','Beautiful','Day!');
echo implode(" ",$arr);
?>
Result: Hello World! Beautiful Day! In the example above, connected implode the $ arr array elements into one string separated by a space before the print screen.