PHP: How to get integer or decimal from a string?

Suppose, I have a string with text and number and I only want the number. I don’t want the characters and text of the string. Here is the way out:- In this example, you will get only integer. It will omit all characters and text from the string. $string = 'Nrs 89,994,874.0098'; echo preg_replace("/[^0-9]/", '', … Read more

PHP: Simple and easy way to format URL string

Here, I will be showing you a simple and easy one line code to format URL string with PHP. By URL string, I mean the url key in any Search Engine Friendly URL. I have used preg_replace function to do so. preg_replace($pattern, $replacement, $string) preg_replace function performs a regular expression search and replace. This will … Read more

PHP: Validate Email, Name, Price, Age using Regular Expression & filter_var

This article contains PHP code with regular expression to validate different numbers and strings. Integer validation can be done for validating age. Decimal validation can be done for validating price. Simple String validation can be done for validating name. Email validation can be done for validating email. preg_match PHP function is used to perform the … Read more

Fun with Regular Expression

Fun with perl regular expression A) $st = “The programming republic of perl”; $1 = ?, $2 = ? and $3 = ? 1) if($st =~/(.*)(e|r)(.*)$/) 2) if($st =~/(m{1,2})(.*)$/) 3) if($st =~/(.*)(m{1,2})(.*)$/) 4) if($st =~/(.?)(m{1,2})(.*)$/) 5) if($st =~/(.+?)(e|r)(.*)$/) 6) if($st =~/(m{1,2}?)(.*?)$/) 7) if($st =~/(m{1,2}?)(.*?)/) 8 ) if($st =~/(.*})(.*)$/) Answer: 1) $1 = The programming republic … Read more