PHP

Function extract image from HTML content using PHP via regex

If you would like to only extract out image from your HTML contents. You may use the function below to do so.

What the codes does is, it will scan through your HTML contents and look for image patterns, and return all of your images in array format.

#—————————————————————————————————-
# Function to get clean image
#—————————————————————————————————-
function getcleanimage($data){
$pattern = “/src=[\"']?([^\"']?.*(png|jpg|gif|jpeg))[\"']?/i”;
preg_match_all($pattern, $data, $images);
return $images;
}
#—————————————————————————————————-

Generate HTML Output via PHP Class

This is a simple example how to set HTML input in your PHP class. There are many ways to write,it, it’s up to you to decide the way of writing it.

PHP file to generate HTML output


Read More…

PHP Shorthand Writing ” if else “

Here is how to write if else shorthand writing. It is useful in your code in order to keep your code neat and tidy. Assume you have a HTML form with input name “WEBSITE_TITLE” which will then be posted to a processing page.

$WEBSITE_TITLE_VAR = !empty($_POST['WEBSITE_TITLE']) ? $_POST['WEBSITE_TITLE'] : null;

 

How it works :

  1. Set your condition at beginning of the statement and the opposite value of the false condition.  Or you can also do it the other way round  which is true -> false OR false ->true.
  2. 1st it will check whether the value of $_POST['WEBSITE_TITLE'] is empty or not.
  3. If $_POST['WEBSITE_TITLE'] is  not empty, then it will hold the posted value
  4. Else “?“ $WEBSITE_TITLE_VAR will be null.
 Scroll to top