PHP preg_match test online and Function Definition


Instructions:


Description and Definition: PHP preg_match is a function that is used when conducting regular expressions match in a string in a PHP programming environment.


The tool below will test any pregmatch condition using PHP.Testing pregmatch is often difficult without real script.There are so many uses of pregmatch function.


We can often see this to validate email address, domain names , etc.So if you are learning PHP programming language, this is one of the most useful functions.


This is very useful if you are formulating a condition to test a string using PHP pregmatch function. For example, say you are testing:


$pattern = "/^([a-z0-9])(([-a-z0-9._])*([a-z0-9]))*\@([a-z0-9])(([a-z0-9-])*([a-z0-9]))+(\.([a-z0-9])([-a-z0-9_-])?([a-z0-9])+)+$/i";



echo preg_match ($pattern, "email-address-to-validate@host.tld");

(Example taken from PHP.net pregmatch page)

The condition is the one inside the double quote:


/^([a-z0-9])(([-a-z0-9._])*([a-z0-9]))*\@([a-z0-9])(([a-z0-9-])*([a-z0-9]))+(\.([a-z0-9])([-a-z0-9_-])?([a-z0-9])+)+$/i



The above example is the use of pregmatch to validate email address. So if you enter the condition to test in the form below, in the next page, you will be
entering the string to test, which is an email address to test with the above condition.


One of the most important use of pregmatch is using it along with PHP conditionals such as an IF- THEN- ELSE statement. Supposing you have dynamic website that is powered with a template and then you need to show something to your user only when the URL is: www.php-developer.org/testpregmatch.php.


In your PHP template (header.php for example if you are using Wordpress CMS), you can execute the following conditionals as follows:


<?php
if (preg_match("/testpregmatch/i",$pageurl)) {
?>

<!--Show HTML code that you would like show only the URL that contains testpregmatch-->

<?php
}
else {
?>

<!--Otherwise show an alternate HTML code if the URL does not contain the string testpregmatch-->

<?php
}
?>



You can test any pregmatch condition below, and the corresponding string to be tested.


Download source code



Step 1: Enter pregmatch condition to test,example(in violet): /^[-a-z._@,'\s]*$/i



Type the captcha below (sorry but this application can be abused by bots, we need to ensure that the user is a human; thanks for understanding):









Link To This Tool
1. Click inside the codebox
2. Right-Click then Copy
3. Paste the HTML code into your webpage
codebox