Monday, 10 March 2014

Expression matching in php

Expression Matching in php programming language. In this tutorials i am briefly preg_match function . preg_match function is used to take expression matching. expression matching basically matches pattern inside string. So, we can use this function to check whether string exist inside of string or we can take more complex pattern and check whether its exists inside strings.

Code for expression matching (Screenshot1) :-

expression matching php
Code for expression Matching Screenshot1

<?php



$string = 'This is a string.';



if(preg_match('/is/',$string)) {

echo 'Match Found.';



} else {

echo 'No match found.';

}



?>

Output (Screenshot2) :-

preg_match function
Screenshot2
Here i am created an example to checking string inside of string. So i had variable string so, now we had to check whether we find a match inside of the string. preg_match function will tell you where the match is its just return you 1 or 0 depending on match. So, we can put in if condition and see what it returns true or false means 0 and 1. In preg_match function take 3 parameters we will take 2 parameters. Here we will searching of string which we had predefined. Here we are searching of is and whole string. You can see match found we know is there inside string.

code for expression matching (Screenshot3) :-

space between string php
Screenshot3

<?php



function has_space($string){

if(preg_match('/ /',$string)) {

return true;

}else {

return false;

}

}



$string = 'Thisdoesnthavespace';



if(has_space($string)) {

echo 'Has at least one space.';

} else {

echo 'Has no spaces.';

}



?>

Output (Screenhsot4) :-

expression matching
Screenshot4
In this example i am creating function and checking whether string had space or not. I had previous  tutorial of Basic Functions in php please check out about how to create functions. Here i had created function named has_space($string) will put string variable and then will check whether condition is true for perg_match or false with two parameters such as space and string which we want to check whether its had a space or not.


No comments:

Post a Comment

Thanks For Comment Will get you Soon..

About Me

Welcome to Extra Tutorials! My name is Mohammed and I am the 22 year writer, website developer, and photographer behind the blog. Thanks for visiting! Tutorials Jackpot… In addition to Developer, I love to develop websites and I love to write. Starting a php Blog was inevitable for me. What began as a simple way to share all of my Tutorials with friends and family has developed into my Part time job.

Mohammed Padela

WHAT IS PHP PROGRAMMING

WHAT IS PHP PROGRAMMING
WHAT IS PHP PROGRAMMING

Follow Us

Popular Posts

Designed ByBlogger Templates