PHP merge arrays recursively of same variable from different file
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()
This is a wrapper tutorial for PHP's curl_multi_exec() function with curl_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 URL
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 anywhere
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 type
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.
XYZ below 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 (via access callback).
Themer-friendly menu link CSS classes for Drupal theme
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.php file of your theme) will provide menu CSS class of <li class="... menu-company-profile">
Style your Drupal theme according to day or night time
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.php file.
Project Euler - Problem 11
Question
In the 20 x 20 grid below, four numbers along a diagonal line have been marked in red (bold).
Project Euler - Problem 10
Question
The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
Find the sum of all the primes below two million.
Solution
$sum = 2;
for ($i = 2; $i < 2000000; $i++) {
if ($i % 2 != 1) {
continue;
}
$d = 3;
$x = sqrt($i);
while (($i % $d != 0) && $d < $x) {
$d += 2;
}
if ((($i % $d == 0 && $i != $d) * 1) == 0) {
$sum += $i;
}
}
echo $sum;Time taken to execute: 30.5757 second.
Project Euler - Problem 9
Question
A Pythagorean triplet is a set of three natural numbers, a a² + b² = c²
For example, 3² + 4² = 9 + 16 = 25 = 5².
There exists exactly one Pythagorean triplet for which a + b + c = 1000.
Find the product abc.