Here, I'll discuss on the usage of Regular Expressions in various validation checking. We are using Javascript Regular expression patterns. These patterns can be tested on any input end-user provides.
Example 1. Input NUMBER
Conditions : 1. The input number must be 8 digit long 2. It should start with '5', '6' or '9'
Code : var pattern = /^[569]{1,1}[0-9]{7,7}$/ var tel_no = "56895869"; // User Input pattern.test(tel_no); // Will return true as Pattern is matching
Example 2. Input STRING
Conditions : 1. The input string should start with the letter 'T' in capital 2. The second letter is a vowel 3. Second letter is small case letter 4. The third letter is any small letter non-digit character 5. The whole string is a 3-letter word.
Code : var pattern = /^T[aeiou][a-z]$/;
Example 3. Input STRING
Conditions : 1. The string should start with any non-digit character including underscore 2. The string can have only underscore as special character. 3. The string can have maximum 10 characters.
Code : var pattern = /^[^0-9_][0-9a-zA-Z_]{0,9}$/;
Example 4. Input STRING
Conditions : 1. The input string must have the word "Band"
Code : var pattern = /\bBand\b/;
Discussions :: The '\b' modifier is used to find word boundaries. That means string like
"Band, great!!!" "Test Band..." "This is my Band. Great na?", "Nice Band!!!" "OK!!Band starts now" "Good Band"
will test true against the pattern. This pattern matches for "Band" followed by a newline character, "Band" word followed by punctuation mark or any such mark followed by the word "Band" etc.
Strings like
"Great Bands" "Bands of Britain"
will always return false because "Band" has not appeared as a single word. Check next part of this article here.
Suppose, the table has a name "animal_table". Now, I want to have all the values in the "Animal_name" column to be merged into a single string. We can do it in MySQL itself with the help of "GROUP_CONCAT" function as shonw below. SELECT group_concat( `Animal_name` SEPARATOR ', ' ) FROM `animal_table` The output is shown below : Lion, Tiger, Monkey, Horse The phrase "SEPARATOR ', '" actually defines the glue string which merges the values together. If we want the whole table sorted before the concatenation, we would write the query as follows. SELECT group_concat( DISTINCT `Animal_name` ORDER BY `Animal_name` SEPARATOR ', ' ) FROM `animal_table` "DISTINCT" has to be used with "ORDER BY" clause.
I have a very small yet so interesting code sample on buffering in PHP. Suppose, I have my web-page divided in two segments.. 1. Main PHP [main.php] for all logic. 2. Template PHP file [template.php], which I include at the bottom of main.php. Main.php ---------- <?php include("template.php"); ?> template.php ---------------- <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> </head> <body> This is a test template. This test template works well. I want to learn more about PHP templates. This test template is finally working perfectly. </body> </html>
Problem :: How to change all occurrences of the word "test template" to "template" without modifying the template file itself? Solution :: We can manipulate the buffer features that comes with PHP and by doing that we can make required changes to the output PHP script generates before rendering onto browser. We need to change the main.php as shown below. <?php function show($buffer) { return str_replace("test template","template",$buffer); } // Start The Buffer with show() function // as a callbackob_start("show"); include("template.php"); // Send the Buffer content to Browser ob_end_flush(); ?> The line "ob_start('show')" line tells PHP server to start buffering the server output and apply the show() function as CallBack on the output. That means, all occurrences of the phrase "test template" are changed to "template" before they are finally sent to browser. And lastly, ob_end_flush() function renders the output to the browser. So, the output of the above script is shown below. This is a template. This template works well. I want to learn more about PHP templates. This template is finally working perfectly.
If you are recieving any Image data from any iPhone or other mobile devices, your image data may come in another hex coded format as displayed below. Here I am showing the methods for :: 1. Creating such hex coded image data from an image file in hard disk 2. Retrieving/recovering back the original image file from that hex coded data. 1. To create such hex coded data, use the following code <?php // READ the image file content and then // strtohex() the data
echo strtohex( file_get_contents( "small.jpg" ) );
function strtohex($dec) { $str = ''; for ($i=0; $i < strlen($dec); $i++) { $k = ""; // Space after every 4 HEX numbers if( $i>0 && $i %4 == 0 ) $k = " "; // GET ASCII $v = dechex( ord( $dec[$i] )); // Prefix a '0' if the HEX number is 1 digit long if(strlen($v) == 1 ) $v = "0".$v;
// Take the whole data in a variable $image_data = "ffd8ffe0 00104a46 49460001 ...." ;
// Remove Spaces $img_data = str_replace(" ","", $image_data);
// Convert From HEX to String $img_data = hextostr($img_data);
//Create a new file $fp = fopen("original.jpg" ,"w+");
//Write data to file fwrite($fp, $img_data);
// Close the file fclose($fp);
function hextostr($hex) { $str = ''; for ($i=0; $i < strlen($hex)-1; $i+=2) { $m = hexdec($hex[$i].$hex[$i+1]); $str .= chr( $m ); } return $str; }
?> The code above is quite self-explanatory. It takes every 2 bytes of image data, converts them to decimal and then a corresponding ASCII character is generated. Check article on How to Convert image data to base64 encoded in PHP.
Here I want to show, how to convert simple image to base64 encoded data which can be used directly with the HTML <img> tag. Step 1 : Create/Get an image which you need to convert into base64-encoded data. Step 2 : Write the following PHP code to effect the conversion. <?php echo $str = base64_encode(file_get_contents("test.jpg")); ?> Step 3 : Base64 encoded data will be echoed on the page. We can use this base64 encoded code in HTML img tag directly as shown below. <img src="data:image/jpg;base64,/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEB AQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH/wAALCAAQAGkBAREA/8QAGwAAAgIDAQAA AAAAAAAAAAAABgcFCAIDBAr/xAAqEAABBQEAAgICAQIHAAAAAAAEAQIDBQYHCBQREgATFhUhJDJi gZGi0f/aAAgBAQAAPwDxFeL+NzPQu54fJa+sS4z1sTZMsK2Qo8NpTR6g8uFqlVc4x8SftihVfWna 75RUX5aqtdcPYcS5re8v6ddL48aXx/s8M2mNo9DdaXWWYOoQq0QAiqhD1IosTY5WzM+FDSYn3CAP kodiOEte7WeD9iX2PGm4jm8K8Zmfi5dJGuvgj/cJK2vm0zmss9I/RqqteU1VAc1zUT7A/X4Y5ZMX hPI6Ot7BaB+P1j1svM9ksMhQ5ik0m5DMr86wH2mujnpyLGWaACaOONSbEcouVTGrIY9WJ8Va8s+V 4/n1rz2wyOZPwTtzjgb6259Znk2ZuUsHSPHlGQqwctk4WdYnvY49fa9ppqKOI1sYYxumW8f+D47m M3V+b23VdT0vN1+0sJP5PZZSuyees5pH1UVRBVrBLdmFhpNNYQnkjMSccNleTAhMz048by3gVxqO xdIGL02l4bygWhPDpVhfVWmjPvi2jVlIUaiQHD1EhcRI81i2OstPSWOVjBSImskyMyHDO28x3Ou5 Xzyy5VreXOrLCyz/APJLPX0+ozlocNXzlLY20qGA2tc/2C3QwjQjIEz/ACEundPVN/d5rxT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xRSRkC IGZEhZzXIjULef8AmNlcr3Xo+utavQWXMtrdT6KsBQWvfoKG+iELFCtxAZrRQIZyQz7GntWwWiRl ClxPf7fpxC/gDiu+cqucyFgeyUWyWnyOlttRzvZYIupF11ARaHsNnqP1WCRioCWUr7VbBpJBoZ0E DBhpI3RziQHk35CZ3q1ViMXkoNibQ4eS6nj1vRLplvr9KReJXTzus44GywheiQNMIOkVgX7YjRZH +m6J0Dadf7r/AMP/APfz/9k=" alt="Test Image"/> Check out the output as shown below.
Problem :: How to create an Image from this base64? This is very simple. <?php // Image Data $image_data = "/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEB AQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH/wAALCAAQAGkBAREA/8QAGwAAAgIDAQAA AAAAAAAAAAAABgcFCAIDBAr/xAAqEAABBQEAAgICAQIHAAAAAAAEAQIDBQYHCBQREgATFhUhJDJi gZGi0f/aAAgBAQAAPwDxFeL+NzPQu54fJa+sS4z1sTZMsK2Qo8NpTR6g8uFqlVc4x8SftihVfWna 75RUX5aqtdcPYcS5re8v6ddL48aXx/s8M2mNo9DdaXWWYOoQq0QAiqhD1IosTY5WzM+FDSYn3CAP kodiOEte7WeD9iX2PGm4jm8K8Zmfi5dJGuvgj/cJK2vm0zmss9I/RqqteU1VAc1zUT7A/X4Y5ZMX hPI6Ot7BaB+P1j1svM9ksMhQ5ik0m5DMr86wH2mujnpyLGWaACaOONSbEcouVTGrIY9WJ8Va8s+V 4/n1rz2wyOZPwTtzjgb6259Znk2ZuUsHSPHlGQqwctk4WdYnvY49fa9ppqKOI1sYYxumW8f+D47m M3V+b23VdT0vN1+0sJP5PZZSuyees5pH1UVRBVrBLdmFhpNNYQnkjMSccNleTAhMz048by3gVxqO xdIGL02l4bygWhPDpVhfVWmjPvi2jVlIUaiQHD1EhcRI81i2OstPSWOVjBSImskyMyHDO28x3Ou5 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TDOWREx8sjBZ/lgzV9W+VufPpLJ+orLdl/b+QNL1kllcKCTUwZ8GN/s14shR8JEtjC97oxRSRkC IGZEhZzXIjULef8AmNlcr3Xo+utavQWXMtrdT6KsBQWvfoKG+iELFCtxAZrRQIZyQz7GntWwWiRl ClxPf7fpxC/gDiu+cqucyFgeyUWyWnyOlttRzvZYIupF11ARaHsNnqP1WCRioCWUr7VbBpJBoZ0E DBhpI3RziQHk35CZ3q1ViMXkoNibQ4eS6nj1vRLplvr9KReJXTzus44GywheiQNMIOkVgX7YjRZH +m6J0Dadf7r/AMP/APfz/9k="; // Create Image File $fp = fopen("new.jpg", "w+"); //Write the new Data fwrite($fp, base64_decode($img_data)); // Close the file fclose($fp); ?>