Activity Stream
48,167 MEMBERS
62853 ONLINE
besthostingforums On YouTube Subscribe to our Newsletter besthostingforums On Twitter besthostingforums On Facebook besthostingforums On facebook groups

Page 1 of 5 123 ... LastLast
Results 1 to 10 of 42
  1.     
    #1
    Respected Developer
    Website's:
    X4B.org

    Default Snippet of the Day

    Disclaimer: Ok, just an idea I thought up. Might take off, might not.

    With that out of the way let me introduce snippet of the day. In this thread you may post one snippet of code a day (at most) that you are particularly proud of.

    Rules:
    • Snippets must be at most 15 lines long. Try to keep them as short as possible.
    • You must have written them yourself and within the course of the day.
    • Any language is allowed
    • No insulting peoples coding. Although that said, if you just started developing in a language this probably isnt the best place to post.
    • Constructuve Critisizm is allowed, just keep it constructive and polite.
    • Links to source are allowed
    • Editing of constants to increase readability is fine.
    • No short replies, use the like feature.
    • CSS/HTML and languages similar can be longer, however a link to a demo site must be provided.
    • A short explanation would also be cool.


    Also remember there is no such thing as perfect code, feel free to post improvements (especially to my code). Could also be called a code review thread, so don't post if you don't want to see your code cut up and reassembled. Its all fun, join in. Share your knowledge.

    The first one, nothing amazing just something to start with. Im sure you have something cooler.

    PHP Code: 
    $sql $this->table()
            ->
    select('MAX(order)')
            ->
    left_join(IP::TABLE)
            ->
    where(array('lease_id'=>$lease->getId(),'ip_version'=>4)); 
    SplitIce Reviewed by SplitIce on . Snippet of the Day Disclaimer: Ok, just an idea I thought up. Might take off, might not. With that out of the way let me introduce snippet of the day. In this thread you may post one snippet of code a day (at most) that you are particularly proud of. Rules: Snippets must be at most 15 lines long. Try to keep them as short as possible. You must have written them yourself and within the course of the day. Any language is allowed No insulting peoples coding. Although that said, if you just started Rating: 5

  2.   Sponsored Links

  3.     
    #2
    (╯?□?)╯︵ ┻━┻
    Website's:
    Xenu.ws WarezLinkers.com SerialSurf.com CracksDirect.com
    Not a bad idea but I doubt many people will post. Especially not enough to keep it active/bumped.

    Either way:

    PHP Code: 
        array_walk($a, function(&$v$k) {
            
    $v parse_url(preg_match('#^http://#'$v) ? $v 'http://' $v);
        }); 
    Just what I happened to have in vim at the minute. Nothing special really, just parses an array of URLs in one go.
    I cut a chunk out to simplify it for posting here, my original code validates each URL instead of assuming ones without 'http' are valid.
    Projects:
    WCDDL - The Professional DDL Script
    Top Secret Project: In Development - ZOMG
    ImgTrack - Never Have Dead Images Again!

  4.     
    #3
    Member
    Building a steam scraper for a client
    Basically the point is to get the appid from the steamstore from a game name which is in the variable $game

    PHP Code: 
    $string file_get_contents('http://store.steampowered.com/search/?snr=1_4_4__12&term='.$game.'');

    preg_match('#http://store.steampowered.com/app/(.*)/?snr=1_7_7_151_150_1#'$string$matches);

    $result substr("$matches[1]"0, -2); 
    Just a short version does more also don't laugh im still learning php
    Signature too big, removed by staff.

  5.     
    #4
    (1only)
    Website's:
    BarakaDesigns.com Amodity.com IMGFlare.com
    Nice idea well its not much but here

    PHP Code: 
    $f array_map('addExt'explode(','$_GET['f']));
    $hash '';
    foreach (
    $f as $files) {
        
    $mtime = @filemtime($files);
        
    $hash .= date('YmdHis'$mtime $mtime NULL).$files; }
    $hash md5($hash); 
    Basically it gets a file extension from addExt (which is a function) which is returned and foreach file that is called will store it uniquely and their change time i usually do this when i either i want to cache something in a basic way not much in the high-end coding with php but at least its something

    CEO Of BarakaDesigns.com, Need PSD to vB? Need a Custom skin? Hit me up -Baraka aka 1Only

  6.     
    #5
    Respected Developer
    Website's:
    X4B.org
    Quote Originally Posted by shadow.prx View Post
    Building a steam scraper for a client
    Basically the point is to get the appid from the steamstore from a game name which is in the variable $game
    Thats fine mate, heres a slightly improved version for you.

    PHP Code: 
    $string file_get_contents('http://store.steampowered.com/search/?snr=1_4_4__12&term='.urlencode($game)); 

    if(
    preg_match('#http://store.steampowered.com/app/(.*)/?snr=1_7_7_151_150_1#'$string$matches)){
         
    $result substr(urldecode($matches[1]), 0, -2); 
    }else
        die(
    'Scrape Failed'); 


    ---------- Post added at 11:11 AM ---------- Previous post was at 10:59 AM ----------

    Quote Originally Posted by 1only View Post
    Nice idea well its not much but here

    Basically it gets a file extension from addExt (which is a function) which is returned and foreach file that is called will store it uniquely and their change time i usually do this when i either i want to cache something in a basic way not much in the high-end coding with php but at least its something
    PHP Code: 
    <?php
    $files 
    array_map('addExt'explode(','$_GET['f']));
    echo 
    splitice_hash($files);

    function 
    splitice_hash($files){
        
    //Calculate
        
    $hash count($files);
        foreach (
    $files as $file) {
            if(
    file_exists($file))
                
    $hash ^= filemtime($file);
        }
        
        
    //Convert to hex, its not as long as MD5 though, you could str_repeat it if hash size is a requirement.
        
    $hash dechex($hash);
    }
    Its not as distributed (security) but as for content matching it should do the job. For a little extra matching ability you could also add a " ^ crc32($file)"

    ---------- Post added at 11:36 AM ---------- Previous post was at 11:11 AM ----------

    Quote Originally Posted by JmZ View Post
    Not a bad idea but I doubt many people will post. Especially not enough to keep it active/bumped.


    Just what I happened to have in vim at the minute. Nothing special really, just parses an array of URLs in one go.
    I cut a chunk out to simplify it for posting here, my original code validates each URL instead of assuming ones without 'http' are valid.
    Oh noes totally going to get banned for this (sarcasm). I guess there may be a reason you didnt use array_map (part of a larger function) but for the benefit of the thread readers.

    PHP Code: 
    array_map(function($v) {
            return 
    parse_url(preg_match('#^http://#si'$v) ? $v 'http://' $v);
        },
    $a); 
    Just a quick explanation of the changes since they are minor:
    1. preg match 's' flag optimizes the expression (good when it needs to be used multiple times)
    2. preg_match 'i' flag for case insensitivity
    3. array_map not array_walk for mapping values (no reference)


    Also just note the parameter order switch, an example of the inconsistencies in PHP.

    Using radical-php if anyone is interested.
    PHP Code: 
    $array = new \Basic\Arr\Object\ArrayObject();
    //...
    $array->Map(function($v) {
            return 
    parse_url(preg_match('#^http://#S'$v) ? $v 'http://' $v);
     }); 
    EDIT 4:
    Actually on second thoughts if you wanted to be pedantic:
    PHP Code: 
    function($v) {
        return 
    parse_url(substr_compare($v'http://'07) ? 'http://' $v $v);

    Although thats taking it too far right? Still I dont think anyone could do it more efficiently (a challenge).

  7.     
    #6
    (╯?□?)╯︵ ┻━┻
    Website's:
    Xenu.ws WarezLinkers.com SerialSurf.com CracksDirect.com
    Quote Originally Posted by SplitIce View Post
    Oh noes totally going to get banned for this (sarcasm). I guess there may be a reason you didnt use array_map (part of a larger function) but for the benefit of the thread readers.

    PHP Code: 
    array_map(function($v) {
            return 
    parse_url(preg_match('#^http://#si'$v) ? $v 'http://' $v);
        },
    $a); 
    Just a quick explanation of the changes since they are minor:
    1. preg match 's' flag optimizes the expression (good when it needs to be used multiple times)
    2. preg_match 'i' flag for case insensitivity
    3. array_map not array_walk for mapping values (no reference)


    Also just note the parameter order switch, an example of the inconsistencies in PHP.

    EDIT 4:
    Actually on second thoughts if you wanted to be pedantic:
    PHP Code: 
    function($v) {
        return 
    parse_url(substr_compare($v'http://'07) ? 'http://' $v $v);

    Although thats taking it too far right? Still I dont think anyone could do it more efficiently (a challenge).
    Here:
    - No, 's' does not make it more efficient, it makes the dot character ('.') include newlines, the default is to exclude newlines
    - Case insensitivity was not needed in my code, it was passed through strtolower beforehand (paths were irrelevant for my usage, so loss of case did not matter)
    - array_walk because I wanted to change the array in place, not allocate memory for a copy of the array and change that instead.

    Doing it more efficiently... well the fact that I used array_walk to change the same array in-place likely outperforms your suggested alternative already. I am not creating any new variables, rather changing the same ones in memory, one by one.

    As for the guy above using regex, use (\d+) not (.*).
    Projects:
    WCDDL - The Professional DDL Script
    Top Secret Project: In Development - ZOMG
    ImgTrack - Never Have Dead Images Again!

  8.     
    #7
    Respected Developer
    Website's:
    X4B.org
    My fault, 'S'. Ill fix that pure typo.
    When a pattern is going to be used several times, it is worth spending more time analyzing it in order to speed up the time taken for matching. If this modifier is set, then this extra analysis is performed. At present, studying a pattern is useful only for non-anchored patterns that do not have a single fixed starting character.
    Guess it wouldn't help you anyway.



    And yep (\d+) would be better persuming steam IDs are numeric (urldecode isnt actually necessary then either. Although im not sure about why the substr is there in that case. Guess we shouldnt improve code we know nothing about. Ey.

  9.     
    #8
    Trusted Webmaster
    Website's:
    KWWHunction.com
    Nice thread you got here Split. It's not only educational but something I haven't seen here on KWWH so

  10.     
    #9
    Member
    Website's:
    PasteBot.appspot.com
    Code: 
    PROJECT_PATH = os.path.abspath(os.path.dirname(__file__))
    Just a little something to get rid of absolute paths.
    Very common in code.
    Extremely useful.

    I didn't invent it. But I did figure it out myself. Reinvented the wheel.

  11.     
    #10
    Respected Developer
    Website's:
    X4B.org
    Yeah very common, in PHP for those who dont know.

    pre 5.3
    PHP Code: 
    $PROJECT_PATH realpath(dirname(__FILE__)); 
    post 5.3
    PHP Code: 
    $PROJECT_PATH realpath(__DIR__); 

Page 1 of 5 123 ... LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Plz Help To Add A Php Snippet Into My DLE Index !
    By JoomlaZ in forum Web Development Area
    Replies: 0
    Last Post: 7th Jul 2011, 01:18 PM
  2. [C#] Tiny Web Server (snippet)
    By Hyperz in forum Web Development Area
    Replies: 6
    Last Post: 24th Jun 2010, 01:19 PM
  3. Image Upload in php. Code snippet #2
    By SplitIce in forum Tutorials and Guides
    Replies: 5
    Last Post: 31st Oct 2009, 07:40 AM
  4. See real OOP (Snippet from Litewarez V2) Webmasters CP
    By litewarez in forum Tutorials and Guides
    Replies: 21
    Last Post: 19th Sep 2009, 03:59 PM
  5. A Snippet from my latest project
    By litewarez in forum Tutorials and Guides
    Replies: 19
    Last Post: 21st Jun 2009, 05:17 PM

Tags for this Thread

BE SOCIAL