Server Side Programming Articles

Page 1014 of 2109

Remove new lines from a string and replace with one empty space PHP?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 858 Views

In PHP, you can remove newlines from a string and replace them with spaces using several methods. The most common approaches are using preg_replace(), str_replace(), or trim() combined with regular expressions. Problem Example Consider this string with newlines ? $sentence = " My name is John Smith My Favorite subject is PHP. "; We need to remove the newlines and replace them with single spaces to get ? My name is John Smith My Favorite subject is PHP. Method 1: Using preg_replace() with trim() This method uses regular expressions ...

Read More

How do I split the letters and numbers to two arrays from a string in PHP

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 623 Views

In PHP, you can split a string containing mixed letters and numbers into separate arrays using preg_split() with regular expressions. This method allows you to extract numbers and letters independently. Using preg_split() with Regular Expressions The approach uses two different regular expression patterns − one to split by digits and extract letters, another to split by letters and extract digits ? Array ( [0] => j [1] => o [2] => h [3] => n ) ...

Read More

Print the time an hour ago PHP?

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

To print time an hour ago in PHP, you need to subtract 1 hour from the current time. Use the strtotime() function with a relative time string like '-1 hour' to calculate the past time. Syntax date('format', strtotime('-1 hour')); Example Here's how to get the time exactly one hour ago ? One hour ago, the date = 2020-09-26 17:42:22 Multiple Hours Ago You can calculate different hours by changing the value ? Current time: 18:42:22 1 hour ago: 17:42:22 ...

Read More

How to call a function from a string stored in a variable PHP?

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

In PHP, you can call a function from a string stored in a variable by simply using the variable as if it were the function name. This technique is known as variable functions and allows for dynamic function calls at runtime. Syntax The basic syntax for calling a function from a string is ? $functionName = 'myFunction'; $functionName(); // Calls myFunction() Example Here's a complete example demonstrating how to call different functions dynamically ? Calling hello method. Calling welcome method. Passing Parameters You can ...

Read More

How to do bold and simple text in the same line using the echo in php?

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

In PHP, you can combine bold and regular text in the same line using HTML tags within your echo statement. The most effective approach is to use the tag for bold text and regular text outside it. Using Strong Tags The simplest way to create bold text alongside regular text is using the HTML tag − This is bold text and this is regular text. Using CSS Styling You can also use CSS font-weight property with tags for more control over styling − ...

Read More

How to deal with multi-byte UTF-8 strings in JavaScript and fix the empty delimiter/separator issue

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 332 Views

In PHP, when working with multi-byte UTF-8 strings, using preg_split() with the '//u' pattern and the PREG_SPLIT_NO_EMPTY flag helps handle empty delimiter issues and properly splits UTF-8 characters. Syntax preg_split('//u', $string, -1, PREG_SPLIT_NO_EMPTY) Parameters The key components of this approach − '//u' − Empty pattern with Unicode modifier for UTF-8 support $string − The input string to split -1 − No limit on number of splits PREG_SPLIT_NO_EMPTY − Removes empty elements from result Example Here's how to split UTF-8 strings into individual characters − ...

Read More

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 199 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 528 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
Showing 10131–10140 of 21,090 articles
Advertisements