Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Articles by AmitDiwan
Page 523 of 840
strlen() php function giving the wrong length of unicode characters ?
The PHP strlen() function counts bytes, not characters, which causes incorrect results with Unicode characters that use multiple bytes. For accurate character counting with Unicode strings, use mb_strlen() instead. Why strlen() Fails with Unicode Unicode characters like accented letters (é, ñ, ȉ) often require 2-4 bytes in UTF-8 encoding. The strlen() function counts these bytes rather than the actual visible characters ? String: JohnSmȉth strlen() result: 10 mb_strlen() result: 9 Comparison of Methods Function What It Counts Unicode Support Result for 'JohnSmȉth' strlen() Bytes No ...
Read MorePHP Why does this && not trigger as false?
In PHP, the logical AND operator (&&) requires all conditions to be true for the overall expression to evaluate as true. If any single condition is false, the entire expression becomes false due to short-circuit evaluation. How && Works The && operator evaluates conditions from left to right and stops as soon as it encounters a false condition. This is called short-circuit evaluation ? The above condition is true Breaking Down the Logic Let's analyze why the above condition evaluates to true ? ...
Read MorePHP make sure string has no whitespace?
To check whether a string contains whitespace in PHP, you can use the preg_match() function with a regular expression pattern. This method detects any whitespace characters including spaces, tabs, and newlines. Syntax preg_match('/\s/', $yourVariableName); Example − Checking for Whitespace The following example demonstrates how to check if a string contains whitespace ? The name (John Smith) has whitespace Example − String Without Whitespace Here's an example with a string that contains no whitespace ? The username (JohnSmith) has no ...
Read MoreHow to add time in minutes in datetime string PHP?
In PHP, you can add minutes to a datetime string using the strtotime() function or the DateTime class. The strtotime() method provides a simple way to parse time expressions and add time intervals. Syntax $result = strtotime('datetime_string + X minute'); You can replace X with any integer value representing the number of minutes to add. Using strtotime() Here's how to add minutes to a datetime string using strtotime() ? 2020-10-30 10:15:20 Using DateTime Class Alternatively, you can use the DateTime class for more object-oriented ...
Read MoreHow to count values from a PHP array and show value only once in a foreach loop?
In PHP, you can count duplicate values in an array and display each unique value only once using the built-in array_count_values() function. This function returns an associative array where keys are the original values and values are their occurrence counts. Using array_count_values() The array_count_values() function automatically counts all occurrences of each value in the array ? Array ( [John] => 1 [David] => 3 [Mike] => 2 ) Using foreach Loop with Counting Logic Alternatively, you can manually count values using a foreach loop ? ...
Read MoreHow to remove a character ('') in string array and display result in one string php?
In PHP, you can remove characters from a string array and display the result as a single string using json_decode() to parse the JSON string, then implode() to join the array elements. Let's say we have the following JSON string array − $full_name = '["John Doe", "David Miller", "Adam Smith"]'; We want to convert this into a single comma-separated string − John Doe, David Miller, Adam Smith Example Here's how to parse the JSON string and convert it to a single formatted string − ...
Read MoreInstance child class in abstract static method PHP?
In PHP, you can create instances of child classes within abstract static methods using the new static() syntax. This approach leverages late static binding to instantiate the actual calling class rather than the abstract parent class. How It Works The static keyword refers to the class that was actually called at runtime, not the class where the code is written. This allows abstract classes to create instances of their concrete child classes. Example The following example demonstrates how to instance a child class in an abstract static method − John ...
Read MoreCombine three strings (day, month, year) and calculate next date PHP?
In PHP, you can combine three separate strings representing day, month, and year to form a complete date, then calculate the next date from a given set of date components. This is useful when working with date arrays or when you need to find the next available date from predefined options. Combining Date Components The most efficient approach is to iterate through arrays of years, months, and days using nested loops to find the next date greater than a given reference date − The next date value is = 2018-05-25 ...
Read MoreRemove new lines from a string and replace with one empty space PHP?
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 MoreHow do I split the letters and numbers to two arrays from a string in PHP
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