Skip to main content

Posts

Showing posts with the label php

How to replace plain URLs with links

Here we will explain how to replace Urls with links from string Using PHP $string ='Rajiv Uttamchandani is an astrophysicist, human rights activist, and entrepreneur. Academy, a nonprofit organization dedicated to providing a robust technology-centered education program for refugee and displaced youth around the world.  CNN Interview - https://www.youtube.com/watch?v=EtTwGke6Jtg   CNN Interview - https://www.youtube.com/watch?v=g7pRTAppsCc&feature=youtu.be'; $string = preg_replace('@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.%-=#]*(\?\S+)?)?)?)@', '<a href="$1">$1</a>', $string); Using Javascript <script> function linkify(inputText) {     var replacedText, replacePattern1, replacePattern2, replacePattern3;     //URLs starting with http://, https://, or ftp://     replacePattern1 = /(\b(https?|ftp):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gim;     replacedText = inputT...

Handling timezone conversion with PHP DateTime

Handling timezone conversion with PHP DateTime Create function function convert_to_server_datetime($date, $userTimeZone = 'America/Los_Angeles', $serverTimeZone = 'UTC') {     try {         $dateTime = new DateTime ($date, new DateTimeZone($userTimeZone));         $dateTime->setTimezone(new DateTimeZone($serverTimeZone));         return $dateTime->format("Y-m-d H:i:s");     } catch (Exception $e) {         return '';     } } Example: $userDate = '2019-04-19 13:20:00'; echo  convert_to_server_datetime ($userDate); Other Method public function convert($clienttimezone = null, $servertimezone = null){ $clientz=timezone_open("$clienttimezone"); $serverdateTime=date_create("now",timezone_open("$servertimezone")); $offset1 = timezone_offset_get($clientz,$serverdateTime); $servertz=timezone_open("$servertimezone"); $clientdateTime=date_crea...

How to create STAR PATTERN in PHP

STAR Pattern Printing using PHP for loop Pattern: * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Code: for($i=0; $i<5; $i++) { for($j=0; $j<5; $j++) { print_r(" * "); } print_r("<br/>"); } Pattern: * * * * * * * * * * * * * * * Code: for($i=0; $i<5; $i++) { for($j=0; $j<=$i; $j++) { print_r(" * "); } print_r("<br/>"); } Pattern:              *          * *        * * *     * * * *  * * * * * Code: for($i=0; $i<5; $i++) { for($j=5; $j>=$i; $j--) { print_r("&nbsp; ");  } for($k=0; $k<=$i; $k++) { print_r("*"); } print_r("<br/>"); } Pattern: * * * * *    * * * *       * * *          * *             * Code: $temp = 1; for($i=5; $i>=1; $i--) { for($k=$temp; $...

6 guaranteed steps how to create CRON JOB FUNTION in wordpress

Create Cron Job function in Wordpress plugin Step 1: Register function on plugin activate  register_activation_hook(__FILE__, 'activate_one'); Step 2: add_filter function for interval //Filter for Adding multiple intervals add_filter( 'cron_schedules', 'intervals_schedule' ); // define interval function   function intervals_schedule($schedules) {   $schedules['everyminute'] = array(    'interval' => 60, // Every 1 minutes    'display'  => __( 'Every 1 minutes' )   );   return $schedules;  } Step 3: Activate hook function   //Schedule a first action if it's not already scheduled   function activate_one() {   if (!wp_next_scheduled('wpyog_cron_action')) {    wp_schedule_event( time(), 'everyminute', 'wpyog_cron_action');   }  } Step 4: Cron hook function   //Hook into that action that'll fire after 1 minutes   add_action('wpyog_cron_action', 'execute_...

PHP Fresher Technical Interview Questions

PHP Fresher Technical Interview Questions Question : How to find out leap year? Answer:  A leap year is a calendar year containing one additional day. It has 366 days instead of the usual 365 days. function isLeapYear($year){   return ((0 == ($year % 4)) && (0 != ($year % 100)) || (0 == ($year % 400))); } var_dump(isLeapYear(2020)); Code Explanation: Every year that is exactly divisible by four is a leap year, except for years that are exactly divisible by 100, but these centurial years are leap years if they are exactly divisible by 400. Question : How to find out Prime number? Answer:   Prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. Function to check whether prime number or not function isPrimeNumber($num){ $flag = 0; for($i=2; $i<=$num/2; ++$i)     {         // condition for nonprime number         if($num%$i==0)   ...

PHP 7 Feautures

Performance Battle, PHP 7 vs. PHP 5 With virtually all updates, minor performance upgrades are to be expected. However, this time PHP brings a significant improvement over earlier versions making sheer performance one of PHP 7’s most attractive features. This comes as part of the “PHPNG” project, which tackles the internals of the Zend Engine itself. By refactoring internal data structures and adding an intermediate step to code compilation in the form of an Abstract Syntax Tree (AST), the result is superior performance and more efficient memory allocation. The numbers themselves look very promising; benchmarks done on real world apps show that PHP 7 is twice as fast as PHP 5.6 on average, and results in 50 percent less memory consumption during requests, making PHP 7 a strong rival for Facebook’s HHVM JIT compiler. Have a look at this infographic from Zend depicting performance for some common CMS and Frameworks. PHP 7 Syntactic Sugar PHP 7 comes with new syntax features. W...

Server Side Datatable handling using PHP and MySql

Server Side Datatable handling using PHP and MySql jQuery datatable server side processing. Using jQuery Ajax and MySql. Step 1: View Layout for display Student Record index.html  <!DOCTYPE html> <html>     <head>         <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />         <title>Server Side Datatable handling</title>         <link rel="stylesheet" id="font-awesome-style-css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" type="text/css" media="all">         <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.9/css/jquery.dataTables.min.css"/>                 <script type="text/javascript" charset="utf8" src="http://ajax.aspnetcdn.com/ajax/jQue...

Create and download Zip folder in php

Create and download Zip folder in php ini_set("memory_limit","1280000M"); ini_set('max_execution_time', 60000); // Zip destination path $zipDestination =  __DIR__.'/images/zipfile.zip'; // Use php zip library $zip = new ZipArchive(); touch($zipDestination); // create zip files if($zip->open($zipDestination, ZipArchive::CREATE)!==TRUE) {     exit("cannot open <$zipDestination>\n"); } // If you want to add all files from folder  $pattern = __DIR__.'/bagImages'; // It will read all files of bagImages folder $files = glob($pattern.'/*.*'); foreach($files as $file) { $fileinfo = pathinfo($file); $zip->addFile($file,$img_fol.'/'.$fileinfo['basename']); } // if you want add sub folder files also use. $rootPath = realpath('images'); $files = new RecursiveIteratorIterator(     new RecursiveDirectoryIterator($rootPath),     RecursiveIteratorIterator::LEAVES_ONLY ); fo...

Access modifier in php

Access modifier in php Restricting access to properties PUBLIC PRIVATE PROTECTED Those class properties and class methods which are set to be PUBLIC is accessed from any where in PHP script.I Access: Every where Those Class properties and class methods which are set to be PRIVATE can only be access with in the class.i.e Only the same class can access the property. Access: This class only Those class properties and class methods which are set to be PROTECTED can only be accessed in side a class and from its child class.i.e only the same class and classes derived from that class can access the property Access: This Class and sub class. public $height; protected $social_insurance; private $pin_number; $abc = new User(); /* Since $pin_number was declared private, this line of code will generate an error. Try it out! */ echo "Tell me private stuff: ".$abc->pin_number; /* Since $pin_number was declared protected, this line of code will genera...

How to setup PayPal Standard Payment integration with PHP

PayPal Standard Payment  I think you have already create business account in PayPal. And setup Instant Payment Notification Preferences Here are simple method for " Instant Payment Notification Preferences " setup Click on edit profile 1. Under ‘ Selling Preferences ’ >> ‘ Instant Payment Notification Preferences ’ a: Set the IPN value to ‘On’ b: Set the IPN URL "http://www.techsudhir.com/ipn.php" HTML Payment Parameter SIMPLE HTML FORM //Set useful variables for paypal form $paypalURL = 'https://www.sandbox.paypal.com/cgi-bin/webscr'; //Test PayPal API URL //$paypalURL = 'https://www.paypal.com/cgi-bin/webscr'; //Live PayPal API URL $paypalID = 'sales@techsudhir.com'; //Business Email <form action="<?php echo $paypalURL; ?>" method="post"> <!-- Identify your business so that you can collect the payments. --> <input type="hidden" name=...