SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Zealot codezilla's Avatar
    Join Date
    Nov 2002
    Location
    upstairs
    Posts
    110
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Regular Expression

    Hi,

    I'm trying to format the request headers:

    HTTP_ACCEPT
    HTTP_ACCEPT_CHARSET
    HTTP_ACCEPT_ENCODING
    HTTP_ACCEPT_LANGUAGE
    HTTP_CACHE_CONTROL
    HTTP_CONNECTION
    HTTP_COOKIE
    HTTP_HOST
    HTTP_KEEP_ALIVE
    HTTP_REFERER
    HTTP_USER_AGENT

    into:

    Accept
    Accept-Charset
    Accept-Encoding
    Accept-Language
    Cache-Control
    Connection
    Cookie
    Host
    Keep-Alive
    Referer
    User-Agent

    Right now I'm doing it like so:

    PHP Code:
    preg_replace(
        
    '/HTTP_(.*)/e'
        
    'str_replace(" ", "-", ucwords(strtolower(str_replace("_", " ", "\1"))))'
        
    array_values(preg_grep('/^HTTP/'array_keys($_SERVER)))); 
    Is there a more efficient/less ugly/better way to do this?

    Thanks!

  2. #2
    Sidewalking anode's Avatar
    Join Date
    Mar 2001
    Location
    Philadelphia, US
    Posts
    2,205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Assuming two str_replaces are less resource intensive than one preg.
    PHP Code:
    $str str_replace('HTTP_'''$str);
    $str str_replace ('_''-'$str);
    $str ucwords(strtolower($str)); 
    TuitionFree a free library for the self-taught
    Anode Says... Blogging For Your Pleasure

  3. #3
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Not bad Anode

    Codezilla - Why the changes anyway ?

  4. #4
    SitePoint Zealot codezilla's Avatar
    Join Date
    Nov 2002
    Location
    upstairs
    Posts
    110
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Two things:

    1) Anode's code is only a partial solution -- though I certainly appreciate the fact that he offered any help at all! I suppose the applied version of his code would be this:

    PHP Code:
    $headers = array();
    foreach (
    $_SERVER as $key => $val)
    {
        if (
    substr($key05) == 'HTTP_')
        {
            
    $str str_replace('HTTP_'''$key);
            
    $str str_replace ('_''-'$str);
            
    $str ucwords(strtolower($str));
            
    $headers[] = $str;
        }

    Unfortunately, this doesn't capitalize the letters after hyphens (e.g. "User-agent" vs. "User-Agent") -- although it's easily correctable, just need to add a line and re-order them. I have no idea if this would be faster than my code or not.

    2) I felt that my code was...well...ugly and that maybe a regular expression with better back-reference handling would get the job done more nicely.

    I'm probably just over thinking things that really aren't that important though.

  5. #5
    Sidewalking anode's Avatar
    Join Date
    Mar 2001
    Location
    Philadelphia, US
    Posts
    2,205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How about:
    PHP Code:
    $headers = array(); 
    foreach (
    $_SERVER as $key => $val) {
     
        if (
    substr($key05) == 'HTTP_') { 
         
    $arr explode('_',strtolower(substr($key5))); 
         
    $str implode('-'array_map('ucwords',$arr));
         
    $headers[] = $str
       } 

    TuitionFree a free library for the self-taught
    Anode Says... Blogging For Your Pleasure


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
  •