php
- Code snippet for calculation of the periodic payment for an annuityPosted on Thursday, 31 January, 2013 - 00:26
<?php // Server side/**
* Calculates the periodic payment for an annuity.
*
* @param float $amount Loan amount.
* @param float $interest Interest rate.
* @param integer $term Loan term (years).
* @return float Loan interest amount.
*/ - Malaysia National Registration Identity Card (NRIC) Parser PHP ClassPosted on Thursday, 31 January, 2013 - 00:24
<?php/**
* @file Malaysia National Registration Identity Card (NRIC) parser.
* @author Leow Kah Thong <http://kahthong.com>
*
* Format of NRIC is "YYMMDD-BP-###G
* YYMMDD - Birth date
* BP - Birth place
* G - Gender
*/class myNric {
- PHP Format Currency to Nearest Thousands such as Kilos, Millions, Billions, and TrillionsPosted on Thursday, 31 January, 2013 - 00:13
Just wanted to share a simple (might be handy) function that I wrote today. Couldn't find much resource on the Internet too strangely.
This will turn your currency number into presentable format.
- PHP merge arrays recursively of same variable from different filePosted on Thursday, 3 May, 2012 - 18:46
Sorry for the long title, I know its sort of confusing. But here's something I came across recently.
If you want to merge 2 array variables of the same name that is being included in a separate file, this tutorial will give you an idea of how to achieve this.
Scenario:
- PHP parallel cURL request with curl_multi_exec() and curl_multi_getcontent()Posted on Monday, 30 April, 2012 - 17:08
This is a wrapper tutorial for PHP's
curl_multi_exec()function withcurl_multi_getcontent()example.Feel free to copy the code example below, play with it, break it, improve it, do whatever you want with it! =)
- PHP and Javascript strip or replace "www" from URLPosted on Monday, 16 April, 2012 - 09:39
Just a handy and short code snippet to demonstrate how to strip or replace "www" from URL but not with "www.tld".
<?php
// Server side
$url = 'http://www.abc.com/?q=Hello+World&os=Win#content';class
Test {
function getHostname($url) {
$parse = parse_url($url);
$host = preg_replace('/^www\.(.+\.)/', '$1', $parse['host']);
return $host;
}
} $test = new Test();
echo $test->getHostname($url);
?>
<!doctype html>
<html>
<head>
</head>
<body>
<!-- Client side -->
<div id="url" style="display:none;"><?php echo $url; ?></div><script>
function getHostname(url) {
var host = url.match('^(?:f|ht)tp(?:s)?\://([^/]+)')[1].toString();
host = host.replace(/^www\.(.+\.)/, '$1');
return host;
}var url = document.getElementById('url').innerHTML;
document.writeln('<br />');
document.writeln(getHostname(url));
</script>
</body>
</html> - Embed Drupal node form anywherePosted on Tuesday, 10 January, 2012 - 13:24
Simple 1 line of code to embed a Drupal node form anywhere in your site although I would recommend doing it the Drupal way which is creating a custom module for this.
print drupal_render(node_add('NODE_TYPE'));Where NODE_TYPE is your content type (machine name).
If the code above returns an error that says the function
node_add()is undefined, add the following code before it. - Display Drupal menu local task tab according to node content typePosted on Sunday, 8 January, 2012 - 16:38
By default every node should have the menu tab (MENU LOCAL TASK) of View and Edit.
If your module wants to add its own tab but only restricted to content type of "foo", then use the following code snippet.
XYZbelow is your page callback for the new custom tab. Basically it calls another function to validate user's permission but we manipulate it to check the node type (viaaccess callback). - Themer-friendly menu link CSS classes for Drupal themePosted on Thursday, 29 December, 2011 - 10:23
What this code does is to give themers an easier way to style individual menu items in Drupal. For example, if you have a menu item title "Company Profile" and you want this link to be red colour, the following code (to be placed inside
template.phpfile of your theme) will provide menu CSS class of<li class="... menu-company-profile"> - Style your Drupal theme according to day or night timePosted on Thursday, 29 December, 2011 - 10:19
Here's a simple snippet to allow you to style your theme differently for day or night time. Place the following code into your theme's
template.phpfile.