#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2010
    Posts
    12
    Rep Power
    0

    Sombody help me please !


    PHP Code:
    $fp=strpos($f,'/')!==false
                    
    ?preg_replace('#(.*/)[^/]*$#','$1',$f
    please somebody help me understand this php and regex line !
  2. #2
  3. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,853
    Rep Power
    6351
    You're missing a significant chunk of this line (the part after the colon, specifically).

    Basically there are a number of things at work here:
    1) Assignment: $fp = {something}; You know this.
    2) strpos: A function that checks to see if one string is in another string, returning false if the search string isn't found.
    3) The ternary operator: $a = $trueFalseCheck ? $valueIfTrue : $valueIfFalse;
    4) preg_replace: Standard regular expression replacement function, read the manual for more.
    5) Whatever is in the "else".

    -Dan
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2010
    Posts
    12
    Rep Power
    0
    PHP Code:
    $fp=strpos($f,'/')!==false
                    
    ?preg_replace('#(.*/)[^/]*$#','$1',$f)
                    :
    ''
    you are right i missed the else statement...
    1)anyway as i understand the strpos return the first char index appearance of '/' in the variable $f (from left to right direction) or false if it doesnt find '/' at all , please correct me if i am wrong ?
    2)if it find any '/' inside the $f then it return the regular expression find and replace function if it doesnt find a '/' then it return '' empty character or null.

    *** now the only thing i dont understand what exactly the preg_replace function do on $f ?
    *** what is the capture group here ?!

    thanks.
  6. #4
  7. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,853
    Rep Power
    6351
    1) Yes, that's true, but this code also runs afoul of PHP's loose type comparisons. Since it uses == instead of ===, 0 also counts as "false," with 0 being the first position in the string. So the "true" case will be evaluated if the string STARTS with /, or has NO / in it. I'm not sure if that was the intention.

    2) Close. If it finds a / at the beginning, or no / at all, do the regexp. Otherwise, return empty string.

    3) Regular expressions are relatively easy to parse once you understand the syntax. This one takes everything up to the LAST slash in the string, and returns it. strings like "/var/log/httpd/error_log.txt" would be changed to "/var/log/httpd/"

    4) The capture group is around (.*/), meaning "any character, any number of times, followed by a slash." Since regular expressions are "greedy" by default, this goes until it hits the last slash.

    It looks like the "== false' is supposed to be "=== 0," since this is manipulating file paths. The intended function of this code is probably:
    PHP Code:
    if ( /*f starts with a slash, therefore it is a file path*/ ) {
       
    $fp "The FOLDER path of the file specified, discarding any filename that may be at the end";
    } else {
        
    $fp "";

    That being said, this whole thing could be replaced with a call to [PHPNET="dirname"]dirname[/PHPNET]

    -Dan
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2010
    Posts
    12
    Rep Power
    0

    hi dan


    yes you are right the preg_replace will run only if the path doesnt has any forward slashes inside it or if it starts with a forward slash, but after i looked in the script i have found that they strip off (remove) preceding slash and also last slash from the path before they use this line of code.

    this is how they remove the preceding and last slashes so it will never has a slash in the first character... :

    PHP Code:
    $f=preg_replace('#^/*|/(/)|/$#','\1',$_REQUEST['f']); 
    so you are right the purpose of this line of code is to find the parent path/folder of the current file/folder and if it is found in the root (with no slashes inside it) it return an empty character....

    the only thing i dont understand is how technically the mechanism of this regular expression work in this case ?!!
    for example lets say $f = '/dir1/dir3/'
    first the code strip off the first and last slashes
    now $f='dir1/dir3'
    after that the code will return the parent path in $fp
    so $fp = 'dir1/'
    what i dont understand understand the sequence or logic of how the preg_replace work on this case 'dir1/dir3'
    what this mean exactly #(.*/)[^/]*$#
    how it work ?
    what is done first (.*/) or [^/] ?
    why they didnt used "or" "|" between the two ?
    what i understand please correct me :
    the preg_replace catch everything
    So : 'dir1/dir3' = #(.*/)[^/]*$#
    and replace it only with the capture group :
    So (.*/) = 'dir1/' So $fp = 'dir1/'
    but what i dont understand why they didnt used "or" "|"
    like when they stripped off the first and last slashes.

    thanks man you deserve a nice cup of beer...catch up
  10. #6
  11. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,853
    Rep Power
    6351
    Use the regex powertoy to determine how regular expressions step through strings.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.

IMN logo majestic logo threadwatch logo seochat tools logo