AmitDiwan has Published 10744 Articles

Convert all types of smart quotes with PHP

AmitDiwan

AmitDiwan

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

828 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 ... Read More

How do you pass objects by reference in PHP 5?

AmitDiwan

AmitDiwan

Updated on 06-Apr-2020 09:19:56

391 Views

A PHP reference is an alias, that allows two different variables to write it to the same value. In PHP version 5, an object variable doesn't contain the object itself as its value. It holds an object identifier that allows object accessors to find the actual object.When an object is ... Read More

How to find PHP execution time?

AmitDiwan

AmitDiwan

Updated on 06-Apr-2020 09:18:38

314 Views

In PHP version 7+, the getrusage function can be used. Below is a sample code demonstration −Example Live Demo//beginning of the script $exec_start = getrusage(); //other code functionalities //end of the script function rutime($ru, $rus, $index) {    return ($ru["ru_$index.tv_sec"]*1000 + intval($ru["ru_$index.tv_usec"]/1000))    - ($rus["ru_$index.tv_sec"]*1000 + intval($rus["ru_$index.tv_usec"]/1000)); } $ru = getrusage(); ... Read More

What is faster: many ifs, or else if in PHP?

AmitDiwan

AmitDiwan

Updated on 06-Apr-2020 09:14:44

816 Views

An else if is a better option.Below is a sample code for multiple if statements −if(condition_A){    //perform some action } if(condition_B){    //perform some action }Below is a sample code for else if statement −if(condition_A){    //perform some action } else if(condition_B){    //perform some action }When else if ... Read More

In php, is 123==0123?

AmitDiwan

AmitDiwan

Updated on 06-Apr-2020 09:13:40

595 Views

The answer is No. This is because 0123 means 123 with base 8 (an octal number) and its equivalent in decimal is 83.Prefixing a number with 0 indicates that it is an octal (base 8) number. This is similar to the fact that 0x indicates hex (base 16) numbers.Consider the ... Read More

Stripping last comma from a foreach loop in PHP?

AmitDiwan

AmitDiwan

Updated on 06-Apr-2020 09:09:58

894 Views

Below is the code that can be used to strip the last comma from the foreach loop −Example Live Demo$result_str = array("Hi", "Hello", "have a", "good day"); foreach ($results as $result) {    $result_str[] = $result->name; } echo implode(", ", $result_str);OutputThis will produce the following output −Hi, Hello, have a, good ... Read More

How can I send radio button value in PHP using JavaScript?

AmitDiwan

AmitDiwan

Updated on 06-Apr-2020 09:08:24

357 Views

ID has to be unique in a document −var option_avail = $('input[name=option_avail]:checked', '#controls').val()     firstoption     secondoption     thirdoption

Split string into sentences using regex in PHP

AmitDiwan

AmitDiwan

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

439 Views

Example Live Demofunction sentence_split($text) {    $before_regexes =       array('/(?:(?:[\'\"„][\.!?…][\'\"”]\s)|(?:[^\.]\s[A-Z]\.\s)|(?:\b(?:St|Gen|Hon|Prof|Dr|Mr|Ms|Mrs|[JS]r|Col|Maj|Brig|Sgt|Capt|Cmnd|Sen|Rev|Rep|Revd)       \.\s)|(?:\b(?:St|Gen|Hon|Prof|Dr|Mr|Ms|Mrs|[JS]r|Col|Maj|Brig|Sgt|Capt|Cmnd|Sen|Rev|Rep|Revd)\.\s[A-Z]\.\s)|(?:\bApr\.\s)|(?:\bAug\.\s)|(?:\bBros\.       \s)|(?:\bCo\.\s)|(?:\bCorp\.\s)|(?:\bDec\.\s)|(?:\bDist\.\s)|(?:\bFeb\.\s)|(?:\bInc\.\s)|(?:\bJan\.\s)|(?:\bJul\.\s)|(?:\bJun\.\s)|(?:\bMar\.\s)|(?       :\bNov\.\s)|(?:\bOct\.\s)|(?:\bPh\.?D\.\s)|(?:\bSept?\.\s)|(?:\b\p{Lu}\.\p{Lu}\.\s)|(?:\b\p{Lu}\.\s\p{Lu}\.\s)|(?:\bcf\.\s)|(?:\be\.g\.\s)|(?:\besp       \.\s)|(?:\bet\b\s\bal\.\s)|(?:\bvs\.\s)|(?:\p{Ps}[!?]+\p{Pe} ))\Z/su',    '/(?:(?:[\.\s]\p{L}{1, 2}\.\s))\Z/su',    '/(?:(?:[\[\(]*\.\.\.[\]\)]* ))\Z/su',       '/(?:(?:\b(?:pp|[Vv]iz|i\.?\s*e|[Vvol]|[Rr]col|maj|Lt|[Ff]ig|[Ff]igs|[Vv]iz|[Vv]ols|[Aa]pprox|[Ii]ncl|Pres|[Dd]ept|min|max|[Gg]ovt|lb|ft|c\.?\s       *f|vs)\.\s))\Z/su',    '/(?:(?:\b[Ee]tc\.\s))\Z/su',    '/(?:(?:[\.!?…]+\p{Pe} )|(?:[\[\(]*…[\]\)]* ))\Z/su',    '/(?:(?:\b\p{L}\.))\Z/su', ... Read More

How to upload large files above 500MB in PHP?

AmitDiwan

AmitDiwan

Updated on 06-Apr-2020 09:01:55

12K+ Views

Large files can be uploaded using PHP in two ways. Both of them are discussed below −By changing the upload_max_filesize limit in the php.ini file.By implementing file chunk upload, that splits the upload into smaller pieces an assembling these pieces when the upload is completed.The php.ini file can be updated ... Read More

How to remove a function at runtime in PHP?

AmitDiwan

AmitDiwan

Updated on 06-Apr-2020 08:51:02

642 Views

The functions and classes in PHP have global scope. This means they could be called outside a function even after they were defined inside the scope and the other way round.But PHP doesn't support function overloading, and it is not possible to redefine previously-declared functions.The function can be defined as ... Read More

Advertisements