Found 1046 Articles for PHP

How to create Web API service in PHP?

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

729 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 functionality can be accessed as shown below −require_once('manage.php'); $m = new manage(12); $m->deleteEntry();How can this be accessed by a different server? A third file can be created that will behave like a buffer/an interface that helps access this data. Below is a sample buffer −Let us call it ‘api/delete.php’require_once('manage.php'); if(hasPermission($_POST['api_key']) ... Read More

Convert spaces to dash and lowercase with PHP

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

2K+ 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

197 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

6K+ 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

230 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

777 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

1K+ 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

3K+ 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

184 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.

Convert all types of smart quotes with PHP

AmitDiwan
Updated on 06-Apr-2020 09:26:09

694 Views

The below lines of code can be used, wherein UTF-8 input is expected.$chr_map = array(    // Windows codepage 1252    "\xC2\x82" => "'", // U+0082⇒U+201A single low-9 quotation mark    "\xC2\x84" => '"', // U+0084⇒U+201E double low-9 quotation mark    "\xC2\x8B" => "'", // U+008B⇒U+2039 single left-pointing angle quotation mark    "\xC2\x91" => "'", // U+0091⇒U+2018 left single quotation mark    "\xC2\x92" => "'", // U+0092⇒U+2019 right single quotation mark    "\xC2\x93" => '"', // U+0093⇒U+201C left double quotation mark    "\xC2\x94" => '"', // U+0094⇒U+201D right double quotation mark    "\xC2\x9B" => "'", // U+009B⇒U+203A single right-pointing angle ... Read More

Advertisements