September 29th, 2006

Find a string within a string in PHP

The PHP function strrpos is great for finding the positiong of a specific character within a string. But that's exactly the problem: that it really only can search for a character. So, digging around the PHP manual, I was able to drum up this handy function, which enables you to search for entire strings, rather than individual characters. This is a recursive function, and is quite efficient.

<?php
//Find last occurance of needle in haystack
function str_rpos($haystack, $needle, $start = 0){
   $tempPos = strpos($haystack, $needle, $start);
   if($tempPos === false){
       if($start == 0){
           //Needle not in string at all
           return false;
       }else{
           //No more occurances found
           return $start - strlen($needle);
       }
   }else{
       //Find the next occurance
       return str_rpos($haystack, $needle, $tempPos + strlen($needle));
   }
}
?>
September 18th, 2006

Macbook Pro for $4.18! Too good to be true?

So, perusing eBay for Macbook Pros, I stumbled upon this auction. Ending in 6 hours, and only one bid for GPB 1.99! How could that be possible?

Well, reading into the auction, I started to become a little skeptical. The following phrases made me question the legitimacy of this auction:
"During to unknown circumstances I am unable to receive mail from "ASK SELLER A QUESTION" so for BUY IT NOW price and TERMS please mail me directly to: molec69@yahoo.com"

Hmm, seems a little funny that of the thousands of eBay sellers, this one in particular was experiencing a major glitch. Also, his grammar stinks, which is a telltale sign of spamming/phishing.

"Before any bid are placed on the auction, you have to contact me !
I will cancel all bid's that have been placed before contact !
"

Riigggghhhhhttt. This would be a first for me. But what the heck, I contacted him, asking if the auction was legit. I received the following reply:

"Thank you for your email.
The Buy Now price is 1,200.00 USD and the shipping will be free.The product is in perfect condition,brand new,with no scratches on it,no dammage, sealed in the original box and it containes all the accesories you need.You will receive the TITLE,owner manual,AND THE PAPERS WITH 3 YEARS INTERNATIONAL WARRANTY!
It will come with waranty fully transferable to your name .
So, if you are agree with the price then I will need your full name and address so I can start the shippment, also all the shipping taxes will be paid by me:
First name:
Last name:
Address:
City:
State:
Zip Code:
Country:
eBay user ID:
Also after that eBay will contact you with the payment details so we can complete the deal.
I hope to hear from you soon.
Thank you.
"

This is one pathetic attempt at scamming on eBay. It's interesting that the user had over 50 good rep auctions to their id. But upon further investigation, it seems they were all purchases, no sales.

I guess it's true what they say: if it seems too good to be true, it most likely is!

September 13th, 2006

Easily create recurring PayPal subscription URLs

Well, I've recently had to create some URLs for people to send me recurring PayPal payments. I find that if you have to modify it at all for a different subscription it can be a pain, gettting lost in the URL encoded string and PayPal parameters. So I whipped up this little JavaScript form to easily craft the recurring payment URLs. Here you go, just bookmark this page, and come back whenever you need a new URL:

September 13th, 2006

Create a feed from Google News on your website

Google News provides an RSS feed for all news items. It can be an excellent source of fresh content on your website. You can copy and paste the PHP code below to embed a feed of the latest Google News items related to the topic of your website.

Make sure you attribute the content in some way to Google, so that you're not violating their TOS.

set_time_limit(0);

$topic = "Business"; // change to the topic of your site
$file = "http://news.google.com/news?sourceid=navclient&ie=UTF-8&rls=GGLG,GGLG:2005-22,GGLG:en&q=".$topic."&output=rss";

$rss_channel = array();
$currently_writing = "";
$main = "";
$item_counter = 0;

function startElement($parser, $name, $attrs) {
        global $rss_channel, $currently_writing, $main;
        switch($name) {
             case "RSS":
             case "RDF:RDF":
             case "ITEMS":
                  $currently_writing = "";
                  break;
             case "CHANNEL":
                  $main = "CHANNEL";
                  break;
             case "IMAGE":
                  $main = "IMAGE";
                  $rss_channel["IMAGE"] = array();
                  break;
             case "ITEM":
                  $main = "ITEMS";
                  break;
             default:
                  $currently_writing = $name;
                  break;
        }
}

function endElement($parser, $name) {
        global $rss_channel, $currently_writing, $item_counter;
        $currently_writing = "";
        if ($name == "ITEM") {

             $item_counter++;
        }
}

function characterData($parser, $data) {
        global $rss_channel, $currently_writing, $main, $item_counter;
        if ($currently_writing != "") {
                switch($main) {
                        case "CHANNEL":
                                if (isset($rss_channel[$currently_writing])) {
                                        $rss_channel[$currently_writing] .= $data;
                                } else {
                                        $rss_channel[$currently_writing] = $data;
                                }
                                break;
                        case "IMAGE":
                                if (isset($rss_channel[$main][$currently_writing])) {
                                        $rss_channel[$main][$currently_writing] .= $data;
                                } else {
                                        $rss_channel[$main][$currently_writing] = $data;
                                }
                                break;
                        case "ITEMS":
                                if (isset($rss_channel[$main][$item_counter][$currently_writing])) {
                                        $rss_channel[$main][$item_counter][$currently_writing] .= $data;
                                } else {
                                        $rss_channel[$main][$item_counter][$currently_writing] = $data;
                                }
                                break;
                }
        }
}

$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");

if (!($fp = fopen($file, "r"))) {
        die("could not open XML input");
}

while ($data = fread($fp, 4096)) {
        if (!xml_parse($xml_parser, $data, feof($fp))) {
                die(sprintf("XML error: %s at line %d",
                                        xml_error_string(xml_get_error_code($xml_parser)),
                                        xml_get_current_line_number($xml_parser)));
        }
}

xml_parser_free($xml_parser);

// below is where you'll modify the default HTML output, and where you can limit the number of items to show from the feed

if (isset($rss_channel["ITEMS"])) {
        if (count($rss_channel["ITEMS"]) > 0) {
                for($i = 0;$i < count($rss_channel["ITEMS"]);$i++) {
                        if (isset($rss_channel["ITEMS"][$i]["LINK"])) {
                        print ("\n<div class=\"itemtitle\"><a href=\"" . "go.php?url=" . $rss_channel["ITEMS"][$i]["LINK"] . "\">" . $rss_channel["ITEMS"][$i]["TITLE"] . "</a></div>");
                        } else {
                        print ("\n<div class=\"itemtitle\">" . $rss_channel["ITEMS"][$i]["TITLE"] . "</div>");
                        }
                         print ("<div class=\"itemdescription\">" . $rss_channel["ITEMS"][$i]["DESCRIPTION"] . "</div><br />");                 }
        } else {
                print ("<b>There are no articles in this feed.</b>");
        }
}

September 12th, 2006

Count the number of words with JavaScript

Sometimes you need a quick way to count the number of words in a string. Suppose you've typed out a blog post or essay and you want to ensure the minimum word count. Well, you could past into Word or OpenOffice, but this is much quicker! This handy little code snippet provides a textarea and the inline JavaScript to display the number of words as you type.

<form name="form_count">
<textarea rows="8" name="word_input" cols="40" wrap="virtual" onkeyup="f=document.form_count;f.word_count.value=f.word_input.value.split(' ').length;"></textarea>
<p><input type="button" value="Count Words" onClick="f=document.form_count;f.word_count.value=f.word_input.value.split(' ').length;">
<input type="text" name="word_count" size="20"></p>
</form>

Here's what it looks like:

« Previous PageNext Page »