Server Side Programming Articles - Page 1839 of 2650

Convert spaces to dash and lowercase with PHP

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

4K+ 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 the following output −hello-have-a-good-day-everyone

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

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

297 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
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
Updated on 07-Apr-2020 11:09:41

341 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 needs to be used with the filter.classes which will extend this one. The RecursiveRegexIterator is extended.abstract class FilesystemRegexFilter extends RecursiveRegexIterator {    protected $regex;    public function __construct(RecursiveIterator $it, $regex) {       $this->regex = $regex;       parent::__construct($it, $regex);    } }They are basic filters and work ... Read More

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

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 day of the month today"; if(date('D', $timestamp) === 'Mon')    echo "It is Monday today";OutputThis will produce the following output −It is the first day of the month today

Create nested JSON object in PHP?

AmitDiwan
Updated on 07-Apr-2020 11:01:47

2K+ Views

JSON structure can be created with the below code −$json = json_encode(array(    "client" => array(       "build" => "1.0",       "name" => "xxxx",       "version" => "1.0"    ),    "protocolVersion" => 4,    "data" => array(       "distributorId" => "xxxx",       "distributorPin" => "xxxx",       "locale" => "en-US"    ) ));

Add PHP variable inside echo statement as href link address?

AmitDiwan
Updated on 07-Apr-2020 10:59:20

4K+ Views

HTML in PHPecho "Link";orecho "Link";PHP in HTML

How do I sort a multidimensional array by one of the fields of the inner array in PHP?

AmitDiwan
Updated on 07-Apr-2020 10:58:27

265 Views

The usort function can be used to sort a multidimensional array. It sorts with the help of a user defined function.Below is a sample code demonstration −Examplefunction compare_array($var_1, $var_2) {    if ($var_1["price"] == $var_2["price"]) {       return 0;    }    return ($var_1["price"] < $var_2["price"]) ? -1 : 1; } usort($my_Array,"compare_array") $var_1 = 2 $var_2 = 0OutputThis will produce the following output −1Explanation − We have declared var_1 and var)2 with integer values. They are compared and the result is returned.

map equal_range() in C++ STL

Ayush Gupta
Updated on 06-Apr-2020 14:19:45

249 Views

In this tutorial, we will be discussing a program to understand map equal_range in C++ STL.This function returns a pair of iterators that bounds the range of the container in which the key equivalent to the given parameter resides.Example Live Demo#include using namespace std; int main() {    //initializing container    map mp;    mp.insert({ 4, 30 });    mp.insert({ 1, 40 });    mp.insert({ 6, 60 });    pair       it;    it = mp.equal_range(1);    cout

Maximum of all Subarrays of size k using set in C++ STL

Ayush Gupta
Updated on 06-Apr-2020 14:16:34

236 Views

In this tutorial, we will be discussing a program to get maximum of all subarrays of size k using set in C++ STL.For this we will be provided with a array of size N and integer K. Our task is to get the maximum element in each K elements, add them up and print it out.Example Live Demo#include using namespace std; //returning sum of maximum elements int maxOfSubarrays(int arr[], int n, int k){    set q;    set::reverse_iterator it;    //inserting elements    for (int i = 0; i < k; i++) {       q.insert(pair(arr[i], i));    } ... Read More

Advertisements