Programming

Getting Warning: split(): REG_BADRPT when using the split PHP function

G

Getting Warning: split(): REG_BADRPT when using the split PHP function when I try to split a string after each question mark (?). E.g. $string = "ABC?DEF?BGT"; list($a,$b) = split('?',$string); Answer: The reason why this is failing is split gives you the power to use regular expressions Use the explode() function instead If you really need to use the split() function try escaping the character...

How to remove all white spaces within a string using PHP

H

There are two functions that could be used namely str_replace and  preg_replace. First, we’ll give an example using str_replace. str_replace is the preferred method since it’s faster and easier to use than preg_replace which relies on regular expressions. str_replace (PHP 4, PHP 5) –  $string =  "This is a test string"; $new_string = str_replace(' ','',$string); echo...

How to alternate between 2 colors using PHP

H

This could come in handy if you have a table with multiple rows and if you wish to alternate between 2 colors. We’ll need to use the Modulus Arithmetic Operator for this (%). Modulus (%) gets the remainder after a division has been made between 2 numbers. eg. 3%2 = 1 Code Example: <table border="0" cellpadding="2" cellspacing="4"> <?php    $rows = 10; //Number of rows   ...

How to get the first element of an array using PHP

H

If you have the array

$array = array('a','b','c','d');

you will need to get the value a from the array
Answer:
Use the array_shift function available in PHP 4 and 5 which shifts an element off the beginning of an array. See example below

$array = array('a','b','c','d');
$first_element = array_shift($array);
echo $first_element;

The above code will output a

How to change the Return-path email address using the PHPMailer class

H

Fill the $Sender property with the email address you wish to have as the Return-path  E.g. $mail->Sender="[email protected]"; This property can be found on Line 97 in the class.phpmailer.php file    /**    * Sets the Sender email (Return-Path) of the message.  If not empty,    * will be sent via -f to sendmail or as 'MAIL FROM' in smtp mode.    * @var string    */   public $Sender            =...

How to validate a date format using PHP

H

To check whether the date entered is in the correct format NOTE: We will use the following date format within this example YYYY-MM-DD Answer: Use the preg_match ‘Perform a regular expression match’  PHP function available for PHP 4 & 5  Find the correct expression And use the following code to check for the date format if(preg_match('/[12]\d{3}-[01]\d-[0123]\d/',$date)){     $msg...

How to compare two dates using PHP

H

First we need to convert the two dates to UNIX Timestamps using the PHP function strtotime Then we can compare the two dates in anyway we wish, in the example below I’m checking that the first date is older than the second date  $date1 = '1999-10-11'; $date2 = '2010-10-11';   $firstDate = strtotime($date1); $secondDate = strtotime($date2);   if($firstDate < $secondDate){      echo 'First...

How to add months to a given date using PHP

H

By using the PHP functions date and strtotime this can be accomplished. Example  $date = '2010-03-22';   $new_date = date('Y-m-d',strtotime('+4 months',strtotime($date)));   echo $new_date; //Displays 2010-07-22 Explanation of PHP functions: 1) FUNCTION date DESCRIPTION string date ( string $format [, int $timestamp ] ) PARAMETERS USED IN THIS EXAMPLE Y = A full numeric representation of a year...

How to check for empty values within a array using PHP

H

If you have an array called $array with the below values Array ( [0] => 1299016800 [1] => [2] => ) As we can see in the above example we have 2 empty values within the array, now how do we determine this using PHP First we get the size of the array $mainSize = sizeof($array); Now we get the size of the array without the empty values $emptySize =sizeof(array_filter($array)); Now...

How to transform a month number to the relevant month name using PHP

H

This can be obtained by combining the date function with the mktime function For example if I want to get Jan from the number 1 which represents the first monthin the year I would use something similar to the below $i = 1; //The number 1 represents January   echo date("M",mktime(0,0,0,$i,1,2010)); //This will output Jan So if you want to output Feb the value of $i would be 2 etc USAGE: date =...

How to auto submit a Sitemap to Google using PHP

H

This can be done via a ping service from Google Webmaster Tools Add the following PHP function  function submitSitemap($site)       {           $url = ''.htmlentities($site.'/sitemap.xml');           $response = file_get_contents($url);                      if($response){             echo $response;           }else{             echo "Failed to submit sitemap";           }       } Now you need to...

How to detect the data type of a PHP variable

H

The data type of a PHP variable can be detected by using the built-in PHP functiongettype() (PHP 4, PHP 5)
Example:

$var ="This is a sentence"

echo $var; //This will return "string"

The following possible values can be returned:

boolean
integer
double
string
array
object
resource
NULL
unknown type

About Author

Ian Carnaghan

I am a software developer and online educator who likes to keep up with all the latest in technology. I also manage cloud infrastructure, continuous monitoring, DevOps processes, security, and continuous integration and deployment.

Follow Me