PHP Articles

Page 26 of 81

Foreach loop with two arrays and if-condition evaluation to find matching values PHP?

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

In PHP, you can use nested foreach loops with if-conditions to find matching values between two arrays. This approach compares each element from the first array against all elements in the second array. Problem Statement Given two arrays, we need to find the common elements between them ? $firstArray = array(10, 20, 30, 40, 50); $secondArray = array(100, 80, 30, 40, 90); Expected output should be ? 30 40 Using Nested Foreach Loops The most straightforward approach uses nested foreach loops to compare each element ? ...

Read More

Get Last Part of URL in PHP?

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

To get the last part of a URL in PHP, you can use several methods depending on your needs. The most common approaches are using basename(), pathinfo(), or regular expressions with preg_match(). Using basename() The simplest method is using PHP's built-in basename() function, which extracts the filename from a path ? The last part is: 9989809898 Using pathinfo() The pathinfo() function provides more detailed information about the URL path ? The last part is: 9989809898 Using Regular Expressions For more ...

Read More

Printing Unwanted Characters with PHP "preg_match"?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 192 Views

To print unwanted characters from a string, use preg_match_all() with a regex pattern that matches non−alphanumeric characters. This function finds all matches and stores them in an array. Syntax preg_match_all(pattern, subject, matches) Parameters The function accepts the following parameters ? pattern − The regular expression pattern to search for subject − The input string to search in matches − Array to store all matches found Example Let's extract special characters from a string containing unwanted symbols ? The original value is: M-y Name/i_s ...

Read More

Search for PHP array element containing string?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 514 Views

In PHP, you can search for array elements containing a specific string using several methods. The most common approaches include preg_match() for pattern matching, array_filter() for functional filtering, and simple string functions like strpos(). Using preg_match() with foreach The preg_match() function allows pattern−based searching with regular expressions ? Array ( [2] => PHP language [3] => Python Language ) Using array_filter() with strpos() A simpler approach using array_filter() for case−insensitive substring matching ? Array ( ...

Read More

Remove comma from a string in PHP?

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

In PHP, you can remove commas from a string using the str_replace() function. This function searches for a specific character or substring and replaces it with another value or removes it entirely. Syntax str_replace(search, replace, subject) Parameters: search − The character or string to find (comma in our case) replace − What to replace it with (empty string to remove) subject − The original string to process Example Let's remove commas from a string containing a person's name ? The actual value is = John, ...

Read More

PHP preg_split with hyphen?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 535 Views

In PHP, you can use preg_split() to split strings at specific patterns, including hyphens. The preg_split() function uses regular expressions to define where the string should be split, giving you more control than simple explode(). Basic Hyphen Splitting To split a string at every hyphen, use a simple pattern ? Array ( [0] => apple [1] => banana [2] => cherry ) Conditional Splitting with Lookahead For more complex splitting, such as splitting only at hyphens followed ...

Read More

How to pass reference parameters PHP?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 591 Views

In PHP, you can pass parameters by reference using the & symbol before the parameter name in the function definition. This allows the function to modify the original variable instead of working with a copy. Syntax To pass a parameter by reference, prefix the parameter with & in the function definition ? function functionName(&$parameter) { // Modify the original variable } Example Here's how to pass a reference parameter to modify the original variable ? The actual value is = 900 The modified ...

Read More

Replacing specific characters from a matched string in PHP regular expression where we don't know the number of instances of the match?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 213 Views

In PHP, you can use preg_replace() with regular expressions to replace specific characters from matched strings, even when you don't know how many instances exist. This is particularly useful for parsing delimited data where certain delimiters need conditional replacement. Problem Example Let's say we have this input string where we want to replace the pipe character "|" only when it appears between two numbers − FirstName|John |LastName|Smith|SalaryProvided|2000|5000 The expected output should replace the "|" between 2000 and 5000 with a space − FirstName|John |LastName|Smith|SalaryProvided|2000 5000 Solution Using preg_replace() The ...

Read More

PHP Inherit the properties of non-class object?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 215 Views

In PHP, you can inherit the structure (property names) of a non-class object while resetting the property values to null. This is useful when you need to create a new object with the same properties but empty values. Method 1: Using clone and foreach Clone the original object and then iterate through its properties to set them to null − Original object: object(stdClass)#1 (2) { ["Name"]=> string(4) "John" ["Age"]=> int(20) } Cloned object: object(stdClass)#2 (2) { ["Name"]=> string(4) "John" ...

Read More

Reading and storing a list as an array PHP

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 231 Views

In PHP, you can read and store a space-separated string of values as an array using several methods. The most efficient approach is using the explode() function, though you can also manually parse using loops. Using explode() Function The simplest and most efficient method to convert a space-separated string into an array ? 10 20 30 40 50 60 Using Manual Parsing with Loop You can also manually parse the string character by character to build the array ? 10 20 30 40 ...

Read More
Showing 251–260 of 802 articles
« Prev 1 24 25 26 27 28 81 Next »
Advertisements