How to Remove the Last Character of a String in PHP

If you need to remove the last character of a string in PHP, then you can do the following: Option 1 – Using rtrim() Syntax: rtrim($string, $character) $mystring = "This is a PHP program!"; echo("Before Removal: $mystring\n"); # Before Removal: This is a PHP program! $newstring = rtrim($mystring, ". "); echo("After Removal: $newstring"); # After Removal: This is a PHP program Option 2 – Using substr() Syntax: substr($string, $start, $length)...

October 28, 2022 · 1 min · 87 words · Andrew

How to Create Function with Multiple Returns in PHP

If you need to create a PHP function that returns multiple values, then you can do one of the following. Option 1 – Returning an array function returnArray() { return array("one", "two"); } // call the function and print the response var_dump(returnArray()); Option 2 – Returning a conditional function returnConditional($x = true) { if ($x) return "one"; else return "two"; } // call the function with a value and print the response var_dump(returnConditional(false)); Option 3 – Using generator to yield values function multipleValues() { yield "one"; yield "two"; } $res = multipleValues(); foreach($res as $r) { echo $r; // first val=="one", second val=="two" }

October 27, 2022 · 1 min · 104 words · Andrew

How to Divide an Array into Chunks in PHP

If you have a single-dimensional array, and want to break it into chunks, you can use the array_chunks method. It takes 3 arguments: The input array / original array The length of the new array (how many dimensions to create) A boolean to preserve the original keys or not <strong>array_chunk</strong>(array&nbsp;$array,&nbsp;int&nbsp;$length,&nbsp;bool&nbsp;$preserve_keys=<strong>false</strong>&nbsp;):array An example of dividing an array into chunks Let’s take an initial starter array with 5 values $input_array = array('a', 'b', 'c', 'd', 'e'); If we said we would like to create 2 chunks:...

May 20, 2021 · 2 min · 238 words · Andrew

How to Strip Script Tags in PHP

If you have some HTML input submitted from a user to your application, before saving it to the database, you may want to strip all <script> tags so that you can prevent cross site scripting attacks and other potential issues. Below we use a Regular Expression to strip the script tag out of a variable. $html = preg_replace('#<script(.*?)>(.*?)</script>#is', '', $html);

May 5, 2021 · 1 min · 60 words · Andrew

Recursively Delete Files and Folders and all Contents using PHP

Below is a quick and easy way to recursively delete all files and folders in a given path using PHP. function destroy_dir($dir) { if (!is_dir($dir) || is_link($dir)) return unlink($dir); foreach (scandir($dir) as $file) { if ($file == "." || $file == "..") continue; if (!destroy_dir($dir."/".$file)) { chmod($dir."/".$file, 0777); if (!destroy_dir($dir."/".$file)) return false; } } return rmdir($dir); } destroy_dir("/var/www/site/public_html/directory/");

June 10, 2020 · 1 min · 58 words · Andrew

How to make a disk cache using PHP

If you have a busy PHP driven website and don’t want to make constant queries to the database for each user that will see the same data, then you can easily resolve this problem by letting the first of these visitors generate a cache for the consecutive visitors. Letting this cache last for a few minutes allows your database to not get overloaded and allows for a much faster website experience as well as a much cheaper hosting bill for you at the end of the month....

April 5, 2020 · 3 min · 612 words · Andrew

How to Follow Redirects with cURL for CLI or PHP

Let’s take a really common example. Say we want to follow redirects with cURL for google.com. It is common to curl follow redirect a URL. Follow Redirects with cURL in the CommandLine If you navigate directly to google in the command-line using curl, you will be taken to a 301 redirect URL. curl google.com #<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8"> #<TITLE>301 Moved</TITLE></HEAD><BODY> #<H1>301 Moved</H1> #The document has moved #<A HREF="http://www.google.com/">here</A>. #</BODY></HTML> This makes sure that you follow the proper canonical URL as set by the particular website in question....

March 1, 2020 · 1 min · 196 words · Andrew

How to build a website quickly using PHP

PHP is a powerful scripting language created for the web. It has come a long way since it’s initial inception in 1994. It was originally created to build websites quickly and easily and has become a lot more than that. While many developers and companies use PHP along with extensive and oftentimes heavy frameworks, there is no reason that you have to do that; you can build websites quickly and very easily by making use of many standard PHP functions....

February 1, 2020 · 2 min · 400 words · Andrew

Create daterange array of missing dates

<?php //fromDate 2014 - 01 - 22 //toDate 2014 - 02 - 20 //$arr Array( [0] = & gt; Array( [ISO_DATE] = & gt; 2014 - 02 - 18[DAY_SUM_AMOUNT] = & gt; 3000[DAY_SUM_VOLUME] = & gt; 2[CONVERSION_PCT] = & gt; 100 ) [1] = & gt; Array( [ISO_DATE] = & gt; 2014 - 02 - 19[DAY_SUM_AMOUNT] = & gt; 4000[DAY_SUM_VOLUME] = & gt; 1[CONVERSION_PCT] = & gt; 100 ) ) //codetime function createDateRangeArray($strDateFrom, $strDateTo) { $aryRange = array(); $iDateFrom = mktime(1, 0, 0, substr($strDateFrom, 5, 2) , substr($strDateFrom, 8, 2) , substr($strDateFrom, 0, 4)); $iDateTo = mktime(1, 0, 0, substr($strDateTo, 5, 2) , substr($strDateTo, 8, 2) , substr($strDateTo, 0, 4)); if ($iDateTo & gt; = $iDateFrom) { array_push($aryRange, date('Y-m-d', $iDateFrom)); // first entry while ($iDateFrom & lt; $iDateTo) { $iDateFrom += 86400; // add 24 hours array_push($aryRange,date('Y-m-d',$iDateFrom)); } } return $aryRange; } function recursive_array_search($needle,$haystack) { foreach($haystack as $key=>$value) { $current_key = $key; if ($needle === $value or (is_array($value) & amp; & amp; recursive_array_search($needle, $value) !...

February 20, 2014 · 2 min · 228 words · Andrew

URL GET vars to PHP Array

Sometimes you will need to retrieve the GET variables passed into the current page URI or you will have a URL string to work from which contains certain GET variables, the below method helps a lot to convert them into an array which you can easily manipulate later. $url = $_SERVER["REQUEST_URI"]; parse_str(parse_url($url, PHP_URL_QUERY), $array); $array is now an array of all the GET variables in the URL. Alternatively you can pass a URI string in place of the $_SERVER[“REQUEST_URI”] by replacing the $url variable with something else....

May 14, 2013 · 1 min · 120 words · Andrew