laxi Posted September 4, 2013 Share Posted September 4, 2013 hi I am new to php. Can someone break the below 2 regular expression and tell me what this will acheive? $managerID = preg_replace('#[^0-9]#i', '', $_SESSION["id"]); $manager = preg_replace('#[^A-Za-z0-9]#i', '', $_SESSION["manager"]); Also wanted to know what does # do? Quote Link to comment https://forums.phpfreaks.com/topic/281848-need-help-to-understand-what-this-mean/ Share on other sites More sharing options...
Solution .josh Posted September 4, 2013 Solution Share Posted September 4, 2013 $managerID = preg_replace('#[^0-9]#i', '', $_SESSION["id"]); This strips anything that is not a number out of the $_SESSION["id"] variable and assigns the results to $managerID. Note: there is an i modifier being used here, which makes the match case-insensitive. This is not necessary for this pattern, since only numbers are expressed. $manager = preg_replace('#[^A-Za-z0-9]#i', '', $_SESSION["manager"]); This strips anything that is not a letter or number out of the $_SESSION["manager"] variable and assigns the results to $manager. Note: there is an i modifier being used here, which makes the match case-insensitive. This is not necessary for this pattern since the pattern already explicitly matches for upper and lowercase letter ranges. Also wanted to know what does # do? This is the pattern delimiter, what specifies the beginning and end of the pattern. You can use pretty much any non-alphanumeric character. A lot of people use / instead because in some languages, that's the only thing available. But using # or ~ is also popular because many times people use regex to parse html/xml content and since / comes up a lot in that content, you have to escape it if you're matching for it within the pattern itself so that php doesn't doesn't get confused by where your pattern actually ends. Quote Link to comment https://forums.phpfreaks.com/topic/281848-need-help-to-understand-what-this-mean/#findComment-1448137 Share on other sites More sharing options...
JonnoTheDev Posted September 4, 2013 Share Posted September 4, 2013 (edited) Try this http://www.myezapp.com/apps/dev/regexp/show.ws Put the regex patter into this website and it will explain it to you. # replace any character that is not in the range 0 - 9 $managerID = preg_replace('#[^0-9]#i', '', $_SESSION["id"]); # replace any character that is not in the range A - Z or a - z or 0 - 9 $manager = preg_replace('#[^A-Za-z0-9]#i', '', $_SESSION["manager"]); The hash character (i.e. '#') at the beginning and end is the indicator for the beginning and end of the regex pattern. Edited September 4, 2013 by neil.johnson Quote Link to comment https://forums.phpfreaks.com/topic/281848-need-help-to-understand-what-this-mean/#findComment-1448139 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.