iPhone Currency Input for Web Apps

I’ve been trying out a smaller JavaScript framework built exclusively for making web apps for mobile use names iWebkit. iWebkit makes it very easy for programmers to build a website that very closely resembles an iPhone App.

After using it for a few days I found myself wanting more functionality from it. Actually, if only two extra features were added to it I would love it so much more.

  • I need a black bottom navigation bar. A lot of iPhone apps use the area for main navigation. Apple’s native IPod app for example uses this area to navigate to
  • The other would be a currency input field. For example the input field starts off with 0.00 and when the user focuses on the input and starts entering in 1,2,3, the input would display, 0.01, 0.12, 1.23. I spoke with the developer via their forum and he confirmed that the functionality does not exist. So I took this on myself.

Since the black navigation bar would be a little more involved, I decided to add the currency input functionality myself. After all, I don’t have that much free time for fun little projects like this.

Below is the JavaScript function that I am calling for a currency input. I put it together pretty quickly so if anyone can think of anything to add to it to make it more complete, it would be greatly appreciated. I am using this code right now live and it appears to be working well.


//
// convertToFloat -
//
// param obj - This it the input box object
// param event - This is the keyboard input allowing us to detect what key was pressed based on the ascii code
// param decimal - The precision of decimal places I am rounding too
//
function convertToFloat(obj, event, decimal) {

var value = obj.value.replace(/[^0-9.]/g,'');
var ascii = event.which;
var convertedNum = parseFloat(value);

if (ascii == 8) { //backspace

if (value == 0) {
convertedNum = 0;
} else {
convertedNum = value/10;
}
// ascii codes between 48 and 57 are numbers
//don't react to other keyboard inputs
} else if (ascii >= 48 && ascii <= 57 && value < 1000000) {
convertedNum = value*10;
}

obj.value = convertedNum.toFixed(decimal);
return;
}


Use PHP to Twitter Using OAuth

Ever since Twitter stop allowing status updates to be posted with some simple php code, my Twitter script has been a big fail whale for everyone. In effort to keep the BrownPHP group happy, here is a little tutorial to post status updates to Twitter with PHP via OAuth.

Link to the BrownPHP Twitter Status Update using OAuth.

Open Authentication Standard Protocol

Now I am not going to get into all the details of “OAuth” or the Open Authentication protocol standard. You can learn more about OAuth and all of its goodness at the OAuth website.

Twitter Authentication Flow Overview

Below is the Flow Overview listed in their documentation that is actually quite helpful to understand the process of authenticating yourself with Twitter.

  1. Build TwitterOAuth object using client credentials.
  2. Request temporary credentials from Twitter.
  3. Build authorize URL for Twitter.
  4. Redirect user to authorize URL.
  5. User authorizes access and returns from Twitter.
  6. Rebuild TwitterOAuth object with client credentials and temporary credentials.
  7. Get token credentials from Twitter.
  8. Rebuild TwitterOAuth object with client credentials and token credentials.
  9. Query Twitter API.

See! Nothing to it!

Pre-Requisites

Once you have completed the Pre-Requisites. Most of the work is actually done.

Folder/File Review

In the twitteroauth code that you have downloaded (and hopefully uploaded to your web server by now) this is the structure that you should see. Included are two folders; images and twitteroauth. Images contains two png files that say, “Sign in with Twitter” that you are free to use with your applications. The twitteroauth folder contains the files that enable the Twitter black magic.

OAuth.php contains many classes used for the OAuth standard. As mentioned above, the goal of this article isn’t to explain OAuth, so just take my word and do not remove this file. The twitteroauth.php file contains the TwitterOAuth class that includes all the methods that you will need for this to work.

config.php is where you will put your consumer key and consumer secret that you should have now after your registered your application with Twitter. Also in this file you will update the OAUTH_CALLBACK define with the url that you want users to be redirected to after they return from Twitter.

callback.php should be the file that you have your config.php file configured to return from Twitter with. This file builds the TwitterOAuth object after the user returns from Twitter.

clearsessions.php does exactly says. It clears the sessions (this will remove the authentication tokens) that have been created and redirects the user to connect.php. connect.php is the start of the example. It basically only has a button (lighter.png/darker.png) to “Sign In with Twitter” that links to redirect.php.

redirect.php is where everything gets started. This file builds the temporary credentials and sends them over to Twitter for authentication. Twitter should then redirect home to the callback.php and then that will redirect to index.php which displays all the information of the user that just logged in. This is nice for showing off all the info that you can pull directly after authenticating with Twitter without having to send any separate requests.

DOCUMENTATION, html.inc, LICENSE, README and test.php are all helpful to skim over, but not really needed for this post.

Link to the BrownPHP Twitter Status Update using OAuth.


Remove Characters from String except ASCII

Recently while developing I came across the need to remove all special characters and numbers from a string. Actually, if it wasn’t an ASCII character, I wanted to get rid of it! I needed this for a method I was working on to use for my Auction Alerts site that I have been working on.

I think the best way to go about handling this would be to use a regular expression. To be more specific, I chose to use ereg_replace. I know that this function has been depreciated in PHP v5.3, meh, it still works like a charm. J

Anyway, an example of the code I used was this:

$cleansed_string = ereg_replace("[^A-Za-z ]", "", $string );

Oops, I forgot to mention that I wanted to leave spaces in it for reasons I’ll leave to your imagination. Glorious right? One line of code to filter all that other junk out. Well, I wasn’t done there.

I also needed this method to take out a number of commonly used words. The goal was to really leave in the words that are truly unique to the string. I don’t care about the words like “the, we, am, I, etc…”

$filter_words = array("the","and","it","we") //there are a lot more words in this array than shown
$filtered_string = str_ireplace($filter_words, " ", $cleansed_string);

It’s really as simple as that. You’ll notice that I used str_ireplace. This is a case-insensitive string replace. “The” is the same to me as “the” so I wanted the function to replace all occurrences found for those.

When all is said and done, $filtered_string is the variable that I returned from the method. It doesn’t have any numbers or special characters in it. Not only that, but it doesn’t have the useless crap words in like “I”.

Remove All Numbers from a String

I didn’t need this, but I thought I would add how to do that from this post. It isn’t much different than removing all characters other than the ASCIIs. All you need to really do is change the regular expression a bit.

$cleansed_string = ereg_replace("[^0-9]", "", $string );

BAMM! Done! Now $cleansed_string contains only numbers from the string.

I hope this little post was helpful you some of you. Let me know if you have any better methods for filtering strings like this.


Welcome to BrownPHP

Welcome to BrownPHP! If this is the first time you have been visiting the site, please make yourself welcome. I made a quick video to go through some of the features of the site like the BrownPHP code repository and the book review section. Check the video out and let me know what you think.

This is the first screencast that I did and it was done in a very short amount of time. I’m just playing with the idea of making more screencasts in the future to possibly run through code an other things like that.

Like I said, this screencast is the first I tried. I’ll most likely redo this in the future so that it is a little more professional, and possibly without all that uh – um, haha. Let me know if there is anything that you would like to see a screencast about in the future.


Crush It! By Gary Vaynerchuk – Reviewed

Crush It!: Why NOW Is the Time to Cash In on Your Passion

I finished reading Crush It: Why NOW Is the Time to Cash In on Your Passion about a week ago. Although I wasn’t overly impressed, it did inspire and re-ignite my passion. The book did certainly have the potential to be better though.

The book primarily focuses on going after your passion so that your job no longer is considered work. It only touches on the strategy to do this with a soft bristled brush however.

Throughout the book, Gary explains that if you follow his methods taught in the book and work hard, you can achieve what he has. What he has is wealth and a job that he enjoys waking up to. He explains how he took his father’s small liquor store and transformed it into a multi-million dollar business. However he never really goes into the methods in detail other than using social networks to assist in branding himself.

I am not by any means a millionaire or consider myself an internet entrepreneurial success as Gary is, but I think that the angle he took in the wine market had more to do with his success than his social networking. If my passion were rodents, I don’t feel I could gather a large following as Mr. Vaynerchuk can reach with a “wine for middle class” approach.

Gary’s points in the book come across arrogant in most cases, but he makes it a point to mention that is just how he is and by trying to fake another personality would be portrayed just as that, fake. Knowing that, I didn’t really mind the name dropping and “I don’t care what anyone thinks” comments. Even though these statements and others like are proudly exclaimed, overall I felt he was sincere and trustworthy throughout the read.

Conclusion:

All in all, the book did inspire me. A couple times throughout my reading, I put the book down and grabbed my laptop to put some work into a few of my sites. Crush it! helped me take a step back and look at the big picture. One could say, I stepped out of the now and into the future. If you feel you are in a rut right now and need something to get you going again this could be the book you need. If you are already all inspired, you may just want to save this book for a rainy day.

I’m be happy to give 3 out of 5 PHP elephants to this book for its ability to inspire creative thinking and emphasis on branding yourself with social media.

Visit Gary Vaynerchuk’s Website to learn more about this author.


  • BrownPHP Tag Cloud

  • Copyright © 1996-2010 Brown PHP. All rights reserved.
    iDream theme by Templates Next | Powered by WordPress

    Powered by Yahoo! Answers