SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Thread: Regex and Json

Hybrid View

  1. #1
    SitePoint Addict
    Join Date
    Jan 2005
    Location
    Paris
    Posts
    370
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Regex and Json

    Hello i have a an application that needs to decide whether to treat a flow of structured data as json or as xml tree based on an anlysis of an url that it calls that should ends by .json or .xml

    i have this piece of code, does that do the trick?

    if (preg_match('#\.([^\.])$#', $url, $format)) {
    $format = $format[1];
    } else {
    $format = 'xml';
    }

    thanks in advance

    Regards
    On a PHP/Java/XML/Javascript/MySQL internship right now!

  2. #2
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,747
    Mentioned
    64 Post(s)
    Tagged
    0 Thread(s)
    $extension = end(explode('.',$url));

    seems easier to me

    Your pattern seems to be correct, though i'm not sure why you'd ever need the else... if a file doesnt end in an extension, you're running either an index file or a script - neither of which would be an XML?

  3. #3
    SitePoint Addict
    Join Date
    Jan 2005
    Location
    Paris
    Posts
    370
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thank you i didn't see that, i think your solution seems easier than doing into the regex game...
    On a PHP/Java/XML/Javascript/MySQL internship right now!

  4. #4
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,396
    Mentioned
    54 Post(s)
    Tagged
    0 Thread(s)
    There are also specific tools available for working with URLs and file paths, to save you needing to use regex/string functions.

    PHP Code:
    $extension pathinfo(parse_url($urlPHP_URL_PATH), PATHINFO_EXTENSION); 
    Salathe
    Software Developer and PHP Manual Author.


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
  •