Results 1 to 10 of 42
-
28th Jun 2012, 02:36 PM #1OPRespected DeveloperWebsite's:
X4B.orgSnippet 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
-
28th Jun 2012, 03:11 PM #2(╯?□?)╯︵ ┻━┻Website's:
Xenu.ws WarezLinkers.com SerialSurf.com CracksDirect.comNot 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);
});
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!
-
28th Jun 2012, 03:49 PM #3Member
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);
Signature too big, removed by staff.
-
28th Jun 2012, 03:55 PM #4(1only)Website's:
BarakaDesigns.com Amodity.com IMGFlare.comNice 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);
-
28th Jun 2012, 04:36 PM #5OPRespected DeveloperWebsite's:
X4B.orgThats 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 ----------
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);
}
---------- Post added at 11:36 AM ---------- Previous post was at 11:11 AM ----------
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);
- preg match 's' flag optimizes the expression (good when it needs to be used multiple times)
- preg_match 'i' flag for case insensitivity
- 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);
});
Actually on second thoughts if you wanted to be pedantic:
PHP Code:function($v) {
return parse_url(substr_compare($v, 'http://', 0, 7) ? 'http://' . $v : $v);
}
-
28th Jun 2012, 05:04 PM #6(╯?□?)╯︵ ┻━┻Website's:
Xenu.ws WarezLinkers.com SerialSurf.com CracksDirect.comHere:
- 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!
-
28th Jun 2012, 05:10 PM #7OPRespected DeveloperWebsite's:
X4B.orgMy 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.
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.
-
28th Jun 2012, 05:29 PM #8Trusted WebmasterWebsite's:
KWWHunction.comNice thread you got here Split. It's not only educational but something I haven't seen here on KWWH so
-
29th Jun 2012, 03:26 AM #9MemberWebsite's:
PasteBot.appspot.comCode:PROJECT_PATH = os.path.abspath(os.path.dirname(__file__))
Very common in code.
Extremely useful.
I didn't invent it. But I did figure it out myself. Reinvented the wheel.
-
29th Jun 2012, 05:14 AM #10OPRespected DeveloperWebsite's:
X4B.org
Sponsored Links
Thread Information
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)
Similar Threads
-
Plz Help To Add A Php Snippet Into My DLE Index !
By JoomlaZ in forum Web Development AreaReplies: 0Last Post: 7th Jul 2011, 01:18 PM -
[C#] Tiny Web Server (snippet)
By Hyperz in forum Web Development AreaReplies: 6Last Post: 24th Jun 2010, 01:19 PM -
Image Upload in php. Code snippet #2
By SplitIce in forum Tutorials and GuidesReplies: 5Last Post: 31st Oct 2009, 07:40 AM -
See real OOP (Snippet from Litewarez V2) Webmasters CP
By litewarez in forum Tutorials and GuidesReplies: 21Last Post: 19th Sep 2009, 03:59 PM -
A Snippet from my latest project
By litewarez in forum Tutorials and GuidesReplies: 19Last Post: 21st Jun 2009, 05:17 PM
themaCreator - create posts from...
Version 3.47 released. Open older version (or...