Category Archives: Code

PHP – Determining the Frequency of a String’s Appearance

The substr_count() function returns the number of times one string occurs within another. Its prototype follows:


int substr_count(string str, string substring)

The following example determines the number of times an IT consultant uses various buzzwords in his presentation:

<?php
$buzzwords = array("mindshare", "synergy", "space");

$talk = "I'm certain that we could dominate mindshare in this space with
our new product, establishing a true synergy between the marketing
and product development teams. We'll own this space in three months.";

foreach($buzzwords as $bw) {
echo "The word $bw appears ".substr_count($talk,$bw)." time(s).<br />";
}
?>

This returns the following:

——————————————–
The word mindshare appears 1 time(s).
The word synergy appears 1 time(s).
The word space appears 2 time(s).
——————————————–

Read more »

PHP – Split text up into 140 char array for Twitter

As you probably know, Twitter only accepts messages of 140 characters or less. If you want to interact with the popular social messaging site, you’ll enjoy this function for sure, which will allow you to truncate your message to 140 characters.

function split_to_chunks($to,$text){
$total_length = (140 - strlen($to));
$text_arr = explode(" ",$text);
$i=0;
$message[0]="";
foreach ($text_arr as $word){
if ( strlen($message[$i] . $word . ' ') >= $total_length ){
          if ($text_arr[count($text_arr)-1] == $word){
$message[$i] .= $word;
} else {
$message[$i] .= $word . ' ';
}
} else {
$i++;
if ($text_arr[count($text_arr)-1] == $word){
$message[$i] = $word;
} else {
$message[$i] = $word . ' ';
}
}
}
return $message;
}

WordPress – Dynamic Highlight Menu

This allows you to theme/style and control the currently selected menu tab in CSS by adding a class=”current” on it.

<ul id="nav">
  <li<?php if ( is_home() || is_category() || is_archive() || is_search() || is_single() || is_date() ) { echo ' class="current"'; } ?>><a href="#">Gallery</a></li>
  <li<?php if ( is_page('about') ) { echo ' class="current"'; } ?>><a href="#">About</a></li>
  <li<?php if ( is_page('submit') ) { echo ' class="current"'; } ?>><a href="#">Submit</a></li>
</ul>

Line 2:

If Home, or Category, or Archive, or Search or Single page is selected, class=”current” will be included in

  • Line 3,4:

    If Page with page slug about or submit is highlighted, class=”current” is added.

    If you are looking at putting categories as menu tabs, here’s how to make the menu dynamic:

    <ul id="nav">
      <li<?php if ( is_category('css') ) { echo ' class="current"'; } ?>><a href="#">CSS</a></li>
      <li<?php if ( is_category(showcase) ) { echo ' class="current"'; } ?>><a href="#">Showcase</a></li>
    </ul>
    

    If category with category slug of css or showcase, class=”current” is added.

  • PHP – Get monday, sunday, last monday & last sunday

    A function I’ve put togheter for a work-related project to get date of monday, sunday, last monday & last sunday.. Might add in next monday and sunday later.

    
    /**
    * Get Mondays and Sundays
    *
    * Get monday, sunday, last monday & last sunday
    * Example usage:
    * // to retreive the dates using today as starting point
    * $mondaysAndSundays = getMondaysAndSundays();
    * // to retreive the dates using a custom date as starting point
    * $mondaysAndSundays = getMondaysAndSundays('1987-04-14');
    *
    * @param date $offset Provide a date from where to calculate from in strtotime() translatable format. If none is given, today's date will be used.
    *
    * @return array
    *
    */
    function getMondaysAndSundays($offset=false){
    
    if(!$offset) $offset = strtotime(date('Y-m-d'));
    else $offset = strtotime($offset);
    
    // this week
    if(date('w',$offset) == 1)
    {
    $mas['monday'] = date('Y-m-d',$offset);
    }
    else{
    $mas['monday'] = date('Y-m-d',strtotime("last Monday",$offset));
    }
    
    if(date('w',$offset) == 6){
    $mas['sunday'] = date('Y-m-d',$offset);
    }
    else{
    $mas['sunday'] = date('Y-m-d',strtotime("next Sunday",$offset));
    }
    
    // last week
    if(date('w',$offset) == 1){
    $mas['lastmonday'] =  date('Y-m-d',strtotime('-1 week',$offset));
    }
    else{
    $mas['lastmonday'] = date('Y-m-d',strtotime('-1 week', strtotime(date('Y-m-d',strtotime("last Monday",$offset)))));
    }
    
    if(date('w') == 6)
    {
    $mas['lastsunday'] = date('Y-m-d',strtotime('-1 week',$offset));
    }
    else
    {
    $mas['lastsunday'] = date('Y-m-d',strtotime("last Sunday",$offset));
    }
    
    return $mas;
    }
    

    WordPress – Create Your Own Popular Posts content in the sidebar

    Highlighting the most popular posts on your site is a great way to send new visitors to your very best content and win them over.

    There are a few different ways you can do this, and quite a few plugins out there to help. But why not base it on the number of comments? If you write a post that makes people talk, it must be a well written post (Most of the time…).

    <h2>Popular Posts</h2>
    <ul>
    <?php $result = $wpdb->get_results("SELECT comment_count,ID,post_title
    FROM $wpdb->posts ORDER BY comment_count DESC LIMIT 0 , 5");
    foreach ($result as $post) {
    setup_postdata($post);
    $postid = $post->ID;
    $title = $post->post_title;
    $commentcount = $post->comment_count;
    if ($commentcount != 0) { ?>
    <li><a href="<?php echo get_permalink($postid); ?>" title="<?php echo
    $title ?>">
    <?php echo $title ?></a> {<?php echo $commentcount ?>}</li>
    <?php } } ?>
    </ul>