php

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 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">

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.

Pages