The json_encode function can be used to add slashes to double quotes. Also ‘addcslahses’ can also be used to add ‘\’ to specific characters −Example Live DemoOutputThis will produce the following output −Hello \there!The ‘addcslashes’ function is used to return a string with backslashes in front of the specific characters. It is a case sensitive function and should usually not be used with 0 (null), r (carriage return), n (newline), f (form read), t (tab), v (vertical tab) values. This is because values like \0, \r, , \t, \f and \v are pre-define escape sequences.In the above code, the ‘addcslashes’ function ... Read More
The GeoIP extension can be used to find the exact location of an IP address. Apart from this, the geoPlugin class can be downloaded from −https://www.geoplugin.com/_media/webservices/geoplugin.class.phpsThe country code list can be found in the below link −https://www.geoplugin.com/iso3166An index.php file can be placed inside the root folder and the below lines of code can be put inside this index file −Once the geoplugin class has been downloaded, a new instance is created and given the name ‘geoplugin’. The locate function is called on this instance of the geoplugin class. The same class object’s countryCode is assigned to a variable named ‘var_country_code’. ... Read More
The unset function can be used to remove array object from a specific index in PHP −Example Live Demo$index = 2; $objectarray = array( 0 => array('label' => 'abc', 'value' => 'n23'), 1 => array('label' => 'def', 'value' => '2n13'), 2 => array('label' => 'abcdef', 'value' => 'n214'), 3 => array('label' => 'defabc', 'value' => '03n2') ); var_dump($objectarray); foreach ($objectarray as $key => $object) { if ($key == $index) { unset($objectarray[$index]); } } var_dump($objectarray);OutputThis will produce the following output −array(4) { [0]=> array(2) { ["label"]=> string(3) "abc" ["value"]=> string(3) "n23" } [1]=> ... Read More
The file_get_contents function takes the name of the php file and reads the contents of the text file and displays it on the console. get the contents, and echo it out.The contents of filename.php would be the output.In the above code, the function ‘file_get_contents’ is called by passing the php file name. The output will be the contents present in the php file.
If the string is pre-formatted, and a plain text representation of the same is needed, it can be wrapped in a HTML tag and html entities can be used to escape the angle brackets. This has been shown below −The string is assigned to a string type and the above is used to show XML in HTML −Example Live DemoOutputThis will produce the following output − EXAMPLE
The ‘array_map’ function sends the value of every element in the array to a user-defined function. It then returns an array with new values, because of calling the user-defined function on the array.Syntax of array_map functionarray_map ( user-defined function, array_1, array_2, array_3…)The user-defined function and the array_1 are compulsory arguments, but array_2 and array_3 are optional.Example Live Demo$result = array( 0=>array('a'=>1, 'b'=>'Hello'), 1=>array('a'=>1, 'b'=>'duplicate_val'), 2=>array('a'=>1, 'b'=>'duplicate_val') ); $unique = array_map("unserialize", array_unique(array_map("serialize", $result))); print_r($unique);OutputThis will produce the following output −Array ( [0] => Array ( [a] => 1 [b] => Hello ) [1] => Array ( [a] => 1 ... Read More
To read last line from file in PHP, the code is as follows −$line = ''; $f = fopen('data.txt', 'r'); $cursor = -1; fseek($f, $cursor, SEEK_END); $char = fgetc($f); //Trim trailing newline characters in the file while ($char === "" || $char === "\r") { fseek($f, $cursor--, SEEK_END); $char = fgetc($f); } //Read until the next line of the file begins or the first newline char while ($char !== false && $char !== "" && $char !== "\r") { //Prepend the new character $line = $char . $line; fseek($f, $cursor--, SEEK_END); $char = fgetc($f); ... Read More
The ‘foreach’ loop can be used to multiple index variables of two arrays. This has been shown below −Example Live DemoOutputThis will produce the following output −aB12 PQ34 cd90 pm49Note − If there are more than 2 arrays, nested ‘foreach’ loops can be used.Here, 2 arrays have been declared and they are being traversed using the ‘foreach’ loop. The result is that the respective index of every array is matched and the data at those indices are displayed one next to the other.
The ‘preg_replace’ function can be used to match the characters in the string and remove the unnecessary characters.To keep letters and numbers −Example Live DemoOutputThis will produce the following output −Hello my name is Bobby I am 8 yearsTo keep letters only −Example Live DemoOutputThis will produce the following output −Hello my name is Bobby I am yearsTo keep letters, numbers and underscoreExampleOutputThis will produce the following output −Hello my name is Bobby I am 8 years
To detect base64 encoding in PHP, the code is as follows −Example Live Demo