Make Sure String Has No Whitespace in PHP

AmitDiwan
Updated on 13-Oct-2020 08:48:42

3K+ Views

To check whether a string has no whitespace, use the preg_match() in PHP.The syntax is as follows preg_match('/\s/',$yourVariableName);ExampleThe PHP code is as follows Live Demo OutputThis will produce the following outputThe name (John Smith) has the space

Add Time in Minutes to DateTime String in PHP

AmitDiwan
Updated on 13-Oct-2020 08:46:42

11K+ Views

For this, you can use the strtotime() method.The syntax is as follows −$anyVariableName= strtotime('anyDateValue + X minute');You can put the integer value in place of X.ExampleThe PHP code is as follows Live Demo OutputThis will produce the following output2020-10-30 10:15:20

Count Values from a PHP Array in a Foreach Loop

AmitDiwan
Updated on 13-Oct-2020 08:44:41

1K+ Views

Let’s say the following is our PHP array $listOfNames = array('John','David','Mike','David','Mike','David');We want the output to display the count of values in the above array like this −Array ( [John] => 1 [David] => 3 [Mike] => 2 )To get the count, use inbuilt function array_count_values().ExampleThe PHP code is as follows Live Demo OutputThis will produce the following outputArray ( [John] => 1 [David] => 3 [Mike] => 2 )

Remove Character in String Array and Display Result in One String in PHP

AmitDiwan
Updated on 13-Oct-2020 08:41:18

322 Views

Let’s say the following is our string array −$full_name= '["John Doe","David Miller","Adam Smith"]';We want the output in a single string −John Doe, David Miller, Adam SmithFor this, use json_decode().ExampleThe PHP code is as follows Live Demo OutputThis will produce the following outputThe Result in one string=John Doe, David Miller, Adam Smith

Instance Child Class in Abstract Static Method in PHP

AmitDiwan
Updated on 13-Oct-2020 08:40:06

550 Views

For this, use $anyObjectName=new static() along with self.ExampleThe PHP code is as follows Live Demo OutputThis will produce the following outputJohn Doe

Generate Array of Random Unique Numbers in PHP

AmitDiwan
Updated on 13-Oct-2020 08:37:21

945 Views

For random unique numbers array, use range() along with shuffle().ExampleThe PHP code is as follows Live Demo OutputThis will produce the following outputArray ( [0] => 11 [1] => 14 [2] => 15 [3] => 12 [4] => 18 )

Combine Day, Month, Year Strings and Calculate Next Date in PHP

AmitDiwan
Updated on 13-Oct-2020 08:35:44

576 Views

You need to iterate using three for loops and lookup in given day, month and year. If the day, month and year is available, then put them into a variable.ExampleThe PHP code is as follows Live Demo OutputThis will produce the following outputThe next date value is =2018-05-25

Remove New Lines from a String and Replace with One Empty Space in PHP

AmitDiwan
Updated on 13-Oct-2020 08:10:30

803 Views

Let’s say the following is our string with new line $sentence = " My name is John Smith My Favorite subject is PHP. ";We need to remove the new line above and replace with a whitespace i.e. the output should be −My name is John Smith My Favourite subject is PHP.For this, use trim() and withing that preg_replace() to replace.ExampleThe PHP code is as follows Live Demo OutputThis will produce the following outputMy name is John Smith My Favorite subject is PHP.

Split Letters and Numbers into Two Arrays from a String in PHP

AmitDiwan
Updated on 13-Oct-2020 08:07:07

559 Views

For this, use preg_split() along with array_shift() and array_pop(). Let’s say the following is our string with letters and numbers $values = "5j4o5h8n";We want to display the numbers and letters in separate arrays.ExampleThe PHP code is as follows Live Demo OutputThis will produce the following outputArray ( [0] => j [1] => o [2] => h [3] => n ) Array ( [0] => 5 [1] => 4 [2] => 5 [3] => 8 )

Print the Time an Hour Ago in PHP

AmitDiwan
Updated on 13-Oct-2020 08:04:40

5K+ Views

To print time an hour ago, you need to subtract -1 hour. Use the strtotime() method and set the subtraction value in it i.e. -1 for 1 hour ago time, -2 for 2 hour ago time, etc.Let’s say the current time is 18:42:22ExampleThe PHP code is as follows Live Demo OutputThis will produce the following outputOne hour ago, the date = 2020-09-26 17:42:22

Advertisements