Server Side Programming Articles

Page 11 of 2108

PHP: Remove object from array

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

In PHP, you can remove objects from an array using the unset() function. This function removes an element at a specific index and preserves the remaining array keys. Using unset() to Remove by Index The simplest way to remove an object from an array is by specifying its index directly − Before removal: array(4) { [0]=> array(2) { ["label"]=> string(3) "abc" ["value"]=> string(3) "n23" } [1]=> ...

Read More

PHP: How do I display the contents of a textfile on my page?

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

In PHP, you can display the contents of a text file on your webpage using the file_get_contents() function. This function reads the entire file into a string and returns it. Basic Usage The simplest way to display file contents is to read the file and echo the result − Example with Error Handling It's good practice to check if the file exists before attempting to read it ? Preserving Formatting If your text file contains line breaks and you want to preserve formatting in HTML, wrap ...

Read More

How to display XML in HTML in PHP?

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

To display XML content within HTML pages using PHP, you need to escape special characters to prevent browser interpretation. The most effective method is using the htmlentities() function combined with HTML tags to preserve formatting. Basic Method The simplest approach uses htmlentities() to convert XML tags into HTML entities ? Sample Data Complete Example Here's a more detailed example showing how to display formatted XML content ? Product ...

Read More

Remove duplicated elements of associative array in PHP

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

In PHP, removing duplicated elements from associative arrays requires special handling since standard functions like array_unique() work only with simple values. For complex associative arrays, you need to serialize the arrays first, remove duplicates, then unserialize them back. Syntax The general approach combines array_map(), serialize(), array_unique(), and unserialize() functions − array_map("unserialize", array_unique(array_map("serialize", $array))) Example Here's how to remove duplicate associative arrays from a multidimensional array − Original array: Array ( [0] => Array ( ...

Read More

Read last line from file in PHP

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

To read the last line from a file in PHP, you can use file pointer manipulation with fseek() and fgetc() to read backwards from the end of the file ? Example The following code demonstrates how to read the last line by starting from the end of the file and reading backwards until a newline character is found ? Last line of the file content will be displayed here How It Works The algorithm works by: Opening the file in read mode using fopen() Setting the cursor ...

Read More

Multiple index variables in PHP foreach loop

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

In PHP, you can iterate through multiple arrays simultaneously using a foreach loop with index variables. This technique allows you to process corresponding elements from different arrays in a single loop. Basic Syntax When using foreach with multiple arrays, you access the index of one array and use it to retrieve elements from other arrays ? aB12 PQ34 cd90 pm49 Multiple Arrays Example You can extend this approach to work with more than two arrays ? Name: John, Age: 25, City: New York ...

Read More

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 739 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 1K+ 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
Showing 101–110 of 21,076 articles
« Prev 1 9 10 11 12 13 2108 Next »
Advertisements