Found 26504 Articles for Server Side Programming

PHP script to get all keys from an array that starts with a certain string

AmitDiwan
Updated on 06-Apr-2020 08:18:02

1K+ Views

Method 1$arr_main_array = array('test_val' => 123, 'other-value' => 456, 'test_result' => 789); foreach($arr_main_array as $key => $value){    $exp_key = explode('-', $key);    if($exp_key[0] == 'test'){       $arr_result[] = $value;    } } if(isset($arr_result)){    print_r($arr_result); }Method 2A functional approach An array_filter_key type of function is taken, and applied to the array elements $array = array_filter_key($array, function($key) {    return strpos($key, 'foo-') === 0; });Method 3A procedural approach −$val_1 = array(); foreach ($array as $key => $value) {    if (strpos($key, 'foo-') === 0) {       $val_1[$key] = $value;    } }Method 4A procedural approach using ... Read More

How to resize image in PHP?

AmitDiwan
Updated on 06-Apr-2020 07:55:41

5K+ Views

Images can be resized using ImageMagick or GD functions. If GD’s functions are used, the size of the image file is also reduced when raw digital camera images are sampled. We will see how GD can be used to resize an image in the below code.function image_resize($file_name, $width, $height, $crop=FALSE) {    list($wid, $ht) = getimagesize($file_name);    $r = $wid / $ht;    if ($crop) {       if ($wid > $ht) {          $wid = ceil($wid-($width*abs($r-$width/$height)));       } else {          $ht = ceil($ht-($ht*abs($r-$w/$h)));       }     ... Read More

Sort multidimensional array by multiple keys in PHP

AmitDiwan
Updated on 06-Apr-2020 07:53:22

3K+ Views

The array_multisort function can be used to sort a multidimensional array based on multiple keys −Example$my_list = array(    array('ID' => 1, 'title' => 'data one', 'event_type' => 'one'),    array('ID' => 2, 'title' => 'data two', 'event_type' => 'zero'),    array('ID' => 3, 'title' => 'data three', 'event_type' => 'one'),    array('ID' => 4, 'title' => 'data four', 'event_type' => 'zero') ); # The list of sorted columns and their data can be obtained. This will be passed to the array_multisort function. $sort = array(); foreach($my_list as $k=>$v) {    $sort['title'][$k] = $v['title'];    $sort['event_type'][$k] = $v['event_type']; } # ... Read More

Convert Dashes to CamelCase in PHP

AmitDiwan
Updated on 06-Apr-2020 07:38:16

1K+ Views

Following is the code to convert dashes to CamelCase in PHP −Sample input − this-is-a-test-stringSample output − thisIsATestStringNote − There is no need to use regex or callbacks. It can be achieved using ucwords.function dashToCamelCase($string, $capitalizeFirstCharacter = false) {    $str = str_replace(' ', '', ucwords(str_replace('-', ' ', $string)));    if (!$capitalizeFirstCharacter) {       $str[0] = strtolower($str[0]);    }    return $str; } echo dashToCamelCase('this-is-a-string');For PHP version>=5.3, the below code can be used −function dashToCamelCase($string, $capitalizeFirstCharacter = false) {    $str = str_replace('-', '', ucwords($string, '-'));    if (!$capitalizeFirstCharacter) {       $str = lcfirst($str);    } ... Read More

How to upload multiple files and store them in a folder with PHP?

AmitDiwan
Updated on 06-Apr-2020 07:36:51

17K+ Views

Below are the steps to upload multiple files and store them in a folder −Input name must be defined as an array i.e. name="inputName[]"Input element should have multiple="multiple" or just multipleIn the PHP file, use the syntax "$_FILES['inputName']['param'][index]"Empty file names and paths have to be checked for since the array might contain empty strings. To resolve this, use array_filter() before count.Below is a demonstration of the code −HTMLPHP$files = array_filter($_FILES['upload']['name']); //Use something similar before processing files. // Count the number of uploaded files in array $total_count = count($_FILES['upload']['name']); // Loop through every file for( $i=0 ; $i < $total_count ; ... Read More

Extract a property from an array of objects in PHP

AmitDiwan
Updated on 06-Apr-2020 07:34:36

4K+ Views

Given the below code, the task is to extract the ID of the my_object variable −Example$my_object = Array ( [0] => stdClass Object    (       [id] => 12    ),    [1] => stdClass Object    (       [id] => 33    ),    [2] => stdClass Object    (       [id] => 59    ) )The array_map function can be used for older versions of PHP. Below is a demonstration of the same.$object_id = array_map(create_function('$o', 'return $o->id;'), $objects);For PHP version 5.5 or higher, the array_column function could be used. Below is a demonstration of the same −$object_id = array_column($my_object, 'id');OutputThis will produce the following output −[12, 33, 59]

How to identify server IP address in PHP?

AmitDiwan
Updated on 06-Apr-2020 07:31:25

4K+ Views

The server IP can be identified with the below line of code −$_SERVER['SERVER_ADDR'];The port can be identified using the below line of code −$_SERVER['SERVER_PORT'];For PHP version 5.3 and higher, the following lines of code can be used −$host_addr= gethostname(); $ip_addr = gethostbyname($host_addr);This can be used when a stand-alone script is being run (which is not running via the web server).

How to force file download with PHP?

AmitDiwan
Updated on 06-Apr-2020 07:30:32

2K+ Views

The below code can be used to force a file to get downloaded in PHP.

Can HTML be embedded inside PHP “if” statement?

AmitDiwan
Updated on 06-Apr-2020 07:27:27

6K+ Views

Yes, HTML can be embedded inside an ‘if’ statement with the help of PHP. Below are a few methods.Using the if condition −    it is displayed iff $condition is met Using the if and else if conditions −     it is displayed iff $condition is met    HTML TAG HERE    HTML TAG HERE Embedding HTML inside PHP − HTML TAG HERE

How to detect search engine bots with PHP?

AmitDiwan
Updated on 06-Apr-2020 07:21:13

1K+ Views

A search engine directory of the spider names can be used as a reference. Next, $_SERVER['HTTP_USER_AGENT']; can be used to check if the agent is a spider (bot).Below is an example demonstrating the same −if(strstr(strtolower($_SERVER['HTTP_USER_AGENT']), "some_bot_name")) {    //other steps that need to be used }Code explanation − The agent, along with the user agent is passed to the strtolower function, whose output in turn is passed to the strstr function. Both the user agent and the bot are compared to see if the spider is a bot or not.Another option is shown below −function _bot_detected() {    return (   ... Read More

Advertisements