Tag Archives: PHP

Drupal 7 – Create your own Rules event

Update @ 2012-10-25: Please make sure you have the PHP filter module enabled. Thanks Beneto.

The Rules module is must have module for developers who needs to deal with some workflow features. Basically each rule contains 3 parts.

  • Event
  • Condition
  • Action

By default the Rules module already provides some basic events, conditions and actions. But some times we may want to create custom components. For me, the most critical part is create a custom event which i would like to show in this post. For the other two, it is less important because the default components already include Execute custom PHP code. That means even without custom module, i could still do whatever i want in Condition and Action.
Continue reading Drupal 7 – Create your own Rules event

Drupal – Embed inline Javascript in node body

Recently i have an client who wants to embeded the FormSite form in the a Drupal 6 website. But after i insert the embedded code in the node body with Full HTML or PHP Code filters. Nothing is shown. For example, if i enter the following code in the node body.
Continue reading Drupal – Embed inline Javascript in node body

PHP – Prevent GIF image loaded from browser cache

I would like to show a GIF image on a website but i found that the animation is not working as the browser caches the GIF image after first load. Here is a PHP workaround by adding the current timestamp to the image src url.

<img src="https://eureka.ykyuen.info/example.gif?v=<?php echo Date("Y.m.d.G.i.s"); ?>" />

 

Done =)

Reference: cache woes, how to force an image to refresh or load fresh

PHP – String startsWith and endsWith

The following 2 functions could check if a string starts/ends with another string.

/* Return TRUE if $needle is empty */
function startsWith($haystack, $needle) {
  $length = strlen($needle);
  return (substr($haystack, 0, $length) === $needle);
}

/* Return TRUE if $needle is empty */
function endsWith($haystack, $needle) {
  $length = strlen($needle);
  if ($length == 0) {
    return TRUE;
  }
  $start  = $length * -1;
  return (substr($haystack, $start) === $needle);
}

Please note that if the $needle is empty, both functions will return TRUE.

Done =)

Reference: StackOverflow – PHP startsWith() and endsWith() functions