Jump to content

need help to understand what this mean


Go to solution Solved by .josh,

Recommended Posts

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?
Link to comment
https://forums.phpfreaks.com/topic/281848-need-help-to-understand-what-this-mean/
Share on other sites

  • Solution

$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.

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 by neil.johnson
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.