AmitDiwan has Published 10744 Articles

Get all the images from a folder in PHP

AmitDiwan

AmitDiwan

Updated on 07-Apr-2020 11:26:55

5K+ Views

The glob function can be used to get the images from a specific folder. Below is a sample code for the same −The path to the image folder is specified and all the files that have a .png extension are extracted. They are displayed with the help of the foreach ... Read More

PHP: Unlink All Files Within A Directory, and then Deleting That Directory

AmitDiwan

AmitDiwan

Updated on 07-Apr-2020 11:24:32

396 Views

Use glob to find all files matching a pattern.function recursive_directory_removal($directory) {    foreach(glob("{$directory}/*") as $file) {       if(is_dir($file)) {          recursive_directory_removal($file);       } else {          unlink($file);       }    }    rmdir($directory); }On PHP version 5.3 and ... Read More

What are the differences in die() and exit() in PHP?

AmitDiwan

AmitDiwan

Updated on 07-Apr-2020 11:22:00

4K+ Views

There is no difference between die and exit, they are the same.The PHP Manual for exit states −"This language construct is equivalent to die()."The PHP Manual for die states −"This language construct is equivalent to exit()."However, there is a small difference, i.e the amount of time it takes for the ... Read More

Search for partial value match in an Array in PHP

AmitDiwan

AmitDiwan

Updated on 07-Apr-2020 11:20:43

1K+ Views

The array_filter function can be used to match a partial value in an array. A callback can be provided, that helps in deciding which elements would remain in the array and which would be removed.When the callback returns false, it means the given element needs to be removed. Below is ... Read More

How to create Web API service in PHP?

AmitDiwan

AmitDiwan

Updated on 07-Apr-2020 11:19:27

883 Views

SOAP and REST APIs are the widely used APIs.Consider the presence of a PHP class named manage.php that helps in managing the entries in a database.class manage { private $entryId; function __construct($entryId) {    $this->entryId = $entryId; } function deleteEntry() {    //delete $this->entryId from database }}On the server, this ... Read More

Convert spaces to dash and lowercase with PHP

AmitDiwan

AmitDiwan

Updated on 07-Apr-2020 11:16:11

3K+ Views

The return value of strtolower can be passed as the third argument to str_replace (where $string is present). The str_replace function is used to replace a set of characters/character with a different set of character/string.Example Live Demo$str = 'hello have a good day everyone'; echo str_replace(' ', '-', strtolower($str));OutputThis will produce ... Read More

PHP file that should run once and delete itself. Is it possible?

AmitDiwan

AmitDiwan

Updated on 07-Apr-2020 11:13:40

292 Views

Yes, it can be done using the unlink function. It has been shown below −Another alternative that deletes the script irrespective of whether the exit function is called or not, has been shown below ^minus;class DeleteOnExit {    function __destruct() {       unlink(__FILE__);    } } $delete_on_exit = new DeleteOnExit();

Parsing JSON array with PHP foreach

AmitDiwan

AmitDiwan

Updated on 07-Apr-2020 11:11:49

7K+ Views

The below code can be used to parse JSON array −Example Live Demo

How to use RegexIterator in PHP?

AmitDiwan

AmitDiwan

Updated on 07-Apr-2020 11:09:41

331 Views

Regular expression$directory = new RecursiveDirectoryIterator(__DIR__); $flattened = new RecursiveIteratorIterator($directory); // Make sure the path does not contain "/.Trash*" folders and ends eith a .php or .html file $files = new RegexIterator($flattened, '#^(?:[A-Z]:)?(?:/(?!\.Trash)[^/]+)+/[^/]+\.(?:php|html)$#Di'); foreach($files as $file) {    echo $file . PHP_EOL; }Using filtersA base class holds the regex that ... Read More

PHP - How to use $timestamp to check if today is Monday or 1st of the month?

AmitDiwan

AmitDiwan

Updated on 07-Apr-2020 11:06:19

1K+ Views

The date function can be used to return the string formatted based on the format specified by providing the integer timestamp or the current time if no timestamp is givenThe timestamp is optional and defaults to the value of time().Example Live Demoif(date('j', $timestamp) === '1')    echo "It is the first ... Read More

Advertisements