Programming Articles

Page 1049 of 2547

Strip punctuation with PHP

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 1K+ Views

In PHP, you can strip punctuation from strings using the preg_replace() function with regular expressions. This function allows you to match specific character patterns and replace them with desired characters or remove them entirely. Keep Letters and Numbers Only To remove all punctuation while keeping alphabetic characters and digits ? This will produce the following output − Hello my name is Bobby I am 8 years Keep Letters Only To remove punctuation and numbers, keeping only alphabetic characters ? This will produce the ...

Read More

Detect base64 encoding in PHP?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 767 Views

In PHP, you can detect base64 encoding by analyzing the character set and using validation functions. Base64 uses only alphanumeric characters, plus signs (+), forward slashes (/), and equal signs (=) for padding. Method 1: Using base64_decode() with Strict Mode The most reliable method uses base64_decode() with the strict parameter set to true −

Read More

How can I remove all empty values when I explode a string using PHP?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 2K+ Views

When exploding a string in PHP, you often need to remove empty values from the resulting array. There are three main approaches: using array_filter(), using array_filter() with strlen, or using preg_split() with the PREG_SPLIT_NO_EMPTY flag. Using array_filter() The simplest approach uses array_filter() to remove falsy values, but this also removes "0" since it evaluates to false − Original string: ,abc, defg, , , hijk, lmnop, , 0, , --- Using array_filter() --- array(4) { [1]=> string(3) "abc" [2]=> string(4) "defg" [5]=> ...

Read More

PHP: fopen to create folders

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 2K+ Views

The fopen() function cannot be used to create directories. This is because fopen() only works with files, not folders. However, you can create directories first using mkdir() and then use fopen() to work with files inside those directories. Why fopen() Cannot Create Directories The fopen() function is specifically designed for file operations. When you try to open a file in a non-existent directory, fopen() will fail and return false. Creating Directories Before Using fopen() Before using fopen(), you should check if the directory exists using is_dir() and create it with mkdir() if necessary − ...

Read More

Check if a PHP cookie exists and if not set its value

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 2K+ Views

In PHP, cookies are available only on subsequent page loads after being set. However, you can check if a cookie exists and set it if it doesn't using isset() with the $_COOKIE superglobal. Understanding Cookie Behavior According to the PHP manual: "Once the cookies have been set, they can be accessed on the next page load with the $_COOKIE or $HTTP_COOKIE_VARS arrays." Cookies are response headers sent to the browser, and the browser must send them back with the next request, making them available only on the second page load. Checking and Setting Cookies You can ...

Read More

How to import csv file in PHP?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 1K+ Views

In PHP, you can import and read CSV files using the built-in fgetcsv() function. This function reads a line from a file pointer and parses it for CSV fields, making it easy to process CSV data row by row. Syntax The fgetcsv() function has the following syntax − fgetcsv($handle, $length, $delimiter, $enclosure, $escape) Parameters $handle − File pointer resource $length − Maximum line length to read $delimiter − Field delimiter (default: comma) $enclosure − Field enclosure character (default: double quote) $escape − Escape character (default: backslash) Example The following ...

Read More

How to validate domain name in PHP?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 2K+ Views

In PHP, you can validate domain names using regular expressions with the preg_match() function. A valid domain name must follow specific rules: contain only alphanumeric characters and hyphens, have proper length limits, and follow DNS naming conventions. Example Here's a function to validate domain names ? The output of the above code is ? Domain: tutorialspoint.com - Valid Domain: sub.example.org - Valid Domain: test-site.net - Valid Domain: https://tutorialspoint.com - Invalid Domain: -invalid.com - Invalid Domain: toolongaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.com - Invalid How It Works The validation function uses three regular expression ...

Read More

How to install Imagick/imagemagick PHP extension on Windows 10?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 351 Views

ImageMagick is a powerful image manipulation library, and the Imagick PHP extension provides an interface to use ImageMagick functions within PHP scripts. Installing it on Windows 10 requires downloading the appropriate binaries and configuring PHP properly. Prerequisites: Ensure you have PHP installed on Windows 10 with a web server like XAMPP, WAMP, or standalone Apache. Step 1: Download ImageMagick First, download the ImageMagick binary for Windows from the official website ? Visit https://imagemagick.org/script/download.php#windows Download the appropriate version matching your PHP architecture (x86 or x64) Install ImageMagick to the default directory (usually C:\Program Files\ImageMagick) ...

Read More

Getting size in memory of an object in PHP?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 2K+ Views

In PHP, you can determine the memory size of an object by using the memory_get_usage() function before and after object creation. This approach measures the difference in memory consumption to estimate the object's size. Basic Memory Measurement The most straightforward method involves capturing memory usage before and after object instantiation − Object memory size: 440 bytes Current memory usage: 2097720 bytes Memory Measurement with Real Memory For more accurate results, use the real_usage parameter to get actual memory allocated by the system − ...

Read More

PHP Call methods of objects in array using array_map?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 1K+ Views

In PHP, you can call methods of objects in an array using array_map() function. This is useful when you need to extract the same property or call the same method on multiple objects. Using Anonymous Functions (PHP 5.3+) The most straightforward approach uses anonymous functions − Array ( [0] => Laptop [1] => Mouse [2] => Keyboard ) Array ( [0] => 999.99 [1] => 25.5 [2] => ...

Read More
Showing 10481–10490 of 25,466 articles
Advertisements