SitePoint Sponsor

User Tag List

Results 1 to 22 of 22
  1. #1
    SitePoint Member Merlin Unknown's Avatar
    Join Date
    Jul 2003
    Location
    CyberSpace
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Simpler Version of Code

    Is there a smaller/more efficient way to write this code:

    PHP Code:
     $name strrev(substr(strstr(strrev(basename($_SERVER['PHP_SELF'])), "."), 1)); 
    This code returns the name of the current file without the extension.

    basename($_SERVER['PHP_SELF'])
    This returns the name of the file with the extension. Is there a better way to parse the string and remove the extension?

    TIA,
    - Merlin Unknown

  2. #2
    SitePoint Zealot
    Join Date
    Jun 2003
    Location
    Middle / East TN
    Posts
    127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $url explode("/",$_SERVER['PHP_SELF']);
    $name $url[count($url)-1]; 
    Smaller but two lines
    There are 10 kinds of people in this world,
    those who can read binary and those who can't.

  3. #3
    La la la la la bronze trophy lieut_data's Avatar
    Join Date
    Jun 2003
    Location
    Waterloo, ON
    Posts
    1,517
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How about
    $url = ereg_replace(".[a-zA-z0-9]+^", "", basename($url));

    Don't shoot me if this doesn't work, I'm relatively new to regular expressions, and this is off the top of my head...
    My name is Steve, and I'm a super-villian.

  4. #4
    SitePoint Zealot
    Join Date
    Jun 2003
    Location
    Middle / East TN
    Posts
    127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I converted mine to one line:
    PHP Code:
    $name end(explode("/",$_SERVER['PHP_SELF'])); 
    There are 10 kinds of people in this world,
    those who can read binary and those who can't.

  5. #5
    La la la la la bronze trophy lieut_data's Avatar
    Join Date
    Jun 2003
    Location
    Waterloo, ON
    Posts
    1,517
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    But your code doesn't remove the extension, as the OT was looking for (at least I don't think it does)
    My name is Steve, and I'm a super-villian.

  6. #6
    SitePoint Member Merlin Unknown's Avatar
    Join Date
    Jul 2003
    Location
    CyberSpace
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I tried both of your suggestions...and thank you for them. But, the code that you gave returns the file name.

    PHP Code:
    $str basename($_SERVER['PHP_SELF']) 
    This code that I had in my first post returns the file name as well, for example: "file.php". I was trying to figure out if there was a more efficient way to truncate the extension, making it return "file" instead of "file.php".

    Any ideas?

    - Merlin Unknown

  7. #7
    La la la la la bronze trophy lieut_data's Avatar
    Join Date
    Jun 2003
    Location
    Waterloo, ON
    Posts
    1,517
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oops, mistyped my regular expression:

    PHP Code:
    $url ereg_replace("\.[a-zA-z0-9]+$"""basename($url)); 
    For "/file/index.php"

    Returns "index"
    My name is Steve, and I'm a super-villian.

  8. #8
    SitePoint Zealot
    Join Date
    Jun 2003
    Location
    Middle / East TN
    Posts
    127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Merlin Unknown
    I tried both of your suggestions...and thank you for them. But, the code that you gave returns the file name.
    Sorry, missed that part, maybe the regex will do it.
    There are 10 kinds of people in this world,
    those who can read binary and those who can't.

  9. #9
    SitePoint Member Merlin Unknown's Avatar
    Join Date
    Jul 2003
    Location
    CyberSpace
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by lieut_data
    Oops, mistyped my regular expression:
    PHP Code:
    $url ereg_replace("\.[a-zA-z0-9]+$"""basename($url)); 
    For "/file/index.php"

    Returns "index"
    I don't know how it works, but it does.

    My final code looked like this:
    PHP Code:
    $shortname ereg_replace("\.[a-zA-z0-9]+$"""basename($_SERVER['PHP_SELF'])); 
    Thanks...

    - Merlin Unknown

  10. #10
    La la la la la bronze trophy lieut_data's Avatar
    Join Date
    Jun 2003
    Location
    Waterloo, ON
    Posts
    1,517
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sometimes regular expresions seem that way to me too, but they can be quite simple:

    basename($url) trims the directories off, and returns just "index.php"

    "\.[a-zA-z0-9]+$"

    "\." - matches a dot
    [a-zA-z0-9]+ - matches any number of letters or numbers
    $ - ensures that this is the end of the string

    I'm starting to like regular expressions
    +$
    My name is Steve, and I'm a super-villian.

  11. #11
    Non-Member Icheb's Avatar
    Join Date
    Mar 2003
    Location
    Germany
    Posts
    1,474
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    basename() accepts a second parameter, which allows you to specify an extension which should be removed.

    PHP Code:
    $bla='dir/dir2/file3.php4';
    echo 
    basename($bla,'.php4'); 
    That will output 'file3' .

  12. #12
    La la la la la bronze trophy lieut_data's Avatar
    Join Date
    Jun 2003
    Location
    Waterloo, ON
    Posts
    1,517
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is there any way to grab *any* extension off the file? I cam across that at first when I looked up his problem, but found it only took off a known extension...

    Cheers!
    My name is Steve, and I'm a super-villian.

  13. #13
    Non-Member Icheb's Avatar
    Join Date
    Mar 2003
    Location
    Germany
    Posts
    1,474
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you are sure you don't have multiple dots in a filename, do this:

    PHP Code:
    $bla='dir/dir2/file3.php4';
    $bla2=basename($bla);
    $bla2=explode('.',$bla2); 
    Then $bla2[0] will contain 'file3' .

  14. #14
    La la la la la bronze trophy lieut_data's Avatar
    Join Date
    Jun 2003
    Location
    Waterloo, ON
    Posts
    1,517
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Good script, what if you *did* have dots?
    My name is Steve, and I'm a super-villian.

  15. #15
    Non-Member Icheb's Avatar
    Join Date
    Mar 2003
    Location
    Germany
    Posts
    1,474
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    RTFM for explode() .

  16. #16
    La la la la la bronze trophy lieut_data's Avatar
    Join Date
    Jun 2003
    Location
    Waterloo, ON
    Posts
    1,517
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Now which one of the following acronyms does RTFM stand for?

    Or am I missing one...

    Look, if you think I'm annoying, it's not because I'm trying to irritate you, but rather to extract from you a complete answer. In regards to the OT, I want to make sure he gets as many good responses as possible. Simply posting a half-solution to his problem won't cut it. That's why I asked from you questions which *would* solve the OT's problem.

    Sure, *I* could have looked up explode (mind you, I wouldn't need to...), but how would that have helped the OT? I would have then been forced to return to the forums, and post my findings. Since clearly you know the answer, I figure *you* ought to provide it....
    Last edited by lieut_data; Jul 24, 2003 at 14:45.
    My name is Steve, and I'm a super-villian.

  17. #17
    SitePoint Zealot
    Join Date
    Jun 2003
    Location
    Middle / East TN
    Posts
    127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $extension = end(explode(".",$url));

    End will get the last item in the array, regardless of how many .'s are in it.
    There are 10 kinds of people in this world,
    those who can read binary and those who can't.

  18. #18
    SitePoint Member Merlin Unknown's Avatar
    Join Date
    Jul 2003
    Location
    CyberSpace
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by lieut_data
    Is there any way to grab *any* extension off the file?
    How about this:
    PHP Code:
    echo basename($_SERVER['PHP_SELF'], strrchr(basename($_SERVER['PHP_SELF']), ".")) 
    More explained version:
    PHP Code:
    // Returns "filename.ANY_EXTENSION"
    $file basename("/dir1/dir2/dir3/filename.ANY_EXTENSION");
     
    // Returns ".ANY_EXTENSION"
    $ext strrchr($file".");
     
    // Returns "filename"
    echo basename($file$ext); 

  19. #19
    Non-Member Icheb's Avatar
    Join Date
    Mar 2003
    Location
    Germany
    Posts
    1,474
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's the thread starter who looked for a solution, not you, and I am trying to help HIM.
    It's not your task to make sure that the thread starter gets what he's looking for, judging from his post I can say he's mature enough to hunt down the answer he's looking for himself.

  20. #20
    "Of" != "Have" bronze trophy Jeff Lange's Avatar
    Join Date
    Jan 2003
    Location
    Calgary, Canada
    Posts
    2,063
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $file basename($_SERVER['PHP_SELF']);
    if (
    false!==($lp strrpos($file'.')))
        
    $name substr($file0$lp);
    else
        
    $name $file
    There we go.


    or for a fast regex using preg_match:
    PHP Code:
    preg_match('/(.*?)(\.[^\.]*)?$/'basename($_SERVER['PHP_SELF']), $match);
    $name $match[1]; 

    Edited them now...
    Last edited by Jeff Lange; Jul 24, 2003 at 15:11.
    Who walks the stairs without a care
    It shoots so high in the sky.
    Bounce up and down just like a clown.
    Everyone knows its Slinky.

  21. #21
    La la la la la bronze trophy lieut_data's Avatar
    Join Date
    Jun 2003
    Location
    Waterloo, ON
    Posts
    1,517
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'll accept that :-)

    Quote Originally Posted by Icheb
    RTFM for explode() .
    I just really hope this was "Read the Friendly manual"...
    My name is Steve, and I'm a super-villian.

  22. #22
    "Of" != "Have" bronze trophy Jeff Lange's Avatar
    Join Date
    Jan 2003
    Location
    Calgary, Canada
    Posts
    2,063
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Note: the first method I posted is about 3x faster than the regex.
    Who walks the stairs without a care
    It shoots so high in the sky.
    Bounce up and down just like a clown.
    Everyone knows its Slinky.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •