PHP Code

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.


Twitter PHP Class Added to Repository

Well I spent a few hours condensing the PHP code that I developed for BrownPHP into a Twitter class that can be downloaded. You can get it PHP class in my code repository.

Take a look at the class and let me know if there are any methods that you would like to have added to the class. Currently there are two main methods.

  1. sendTweet – This method is a popular one that is seems like everyone want to use. It’s a simple method that can be called to tweet something to your Twitter account. This method is very simple and shouldn’t be complicated at all to implement into your own code at all.
  2. getTagCloud – This is another easily implemented method. By providing any Twitter username, this method will generate a Twitter Tag Cloud. If you have never used a Twitter Tag Cloud, it basically can provide a bird’s eye view of the topics that a Twitter user tweets out more regularly.

I won’t get into how integrating your own sites and projects into Twitter can make them more social and viral inducing, but I will say that if you aren’t using Twitter right now you are losing out on a great way to help your brand.

There are so many insightful people on Twitter (granted a lot of trolls too unfortunately) and a lot of great ideas that can culminate by socializing with this community. I myself am going through a re-branding phase. Instead of using my dlbrown06 Twitter account, I will be using the BrownPHP Twitter account to be tweeting about BrownPHP and other PHP interests.

If you are involved in the PHP community, wanting to learn PHP, or are a PHP guru yourself follow BrownPHP. I’ll be sure to follow you as well to continue discussing PHP projects and development.


Use PHP to Twitter Your Tweets

Are You Active on Twitter?

View Results

Loading ... Loading ...

Show the Twitter Love! :)

Use PHP to Twitter Your Tweets
I recently got an email from a reader asking me if I could help them with a small script to post updates to a Twitter account using PHP. I figured this might be something that a few others might be interested in, so I decided to post about it.

As most of you know, there are a multitude of options when deciding how to to Tweet to Twitter. If you would like to add this functionality to your site so that you don’t have to download an application to your computer, or visit another site, this might be something you would like.

Using the simple script below you, you can post updates to twitter. Please BE ADVISED: this script needs altered to run. As well as some extra code to add your desired functionality.

$username = 'myUserName';
$password = 'myPassword';
$status = urlencode(stripslashes(urldecode('This is a new Tweet!')));

if ($status) {
$tweetUrl = 'http://www.twitter.com/statuses/update.xml';

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "$tweetUrl");
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, "status=$status");
curl_setopt($curl, CURLOPT_USERPWD, "$username:$password");

$result = curl_exec($curl);
$resultArray = curl_getinfo($curl);

if ($resultArray['http_code'] == 200)
echo 'Tweet Posted';
else
echo 'Could not post Tweet to Twitter right now. Try again later.';

curl_close($curl);
}

Needed Alterations:

  • Change $username to YOUR username
  • Change $password to YOUR password
  • Edit $status to the Tweet that you want posted.

Code to Add:

  • Some HTML to Tweet dynamically. For example: Use a form like below and have $_POST['new_tweet'] update the $status variable.
  • More Filtering: It would probably be a good idea to add some more filtering to the status so that you can’t just post a blanks status etc. (I could add that easily, but then where would be your opportunity to learn!)
  • Use OOP Practices: Creating a Twitter Class out of the below procedural code would make it much more modular for you in the future. (Again, better for you to do this yourself. Feel free to contact me with any questions.)
  • Add Some Javascript: Adding some Javascript that counts the amount of characters in the Tweet field would be handy to have for your visitors.

Try it out for yourself (Your Twitter Username and Password are NOT being recorded):

Tweet from BrownPHP

Username:

Password:

Tweet:

Tweet from BrownPHP if you like to test it out for yourself. You can also just copy this code, and make the necessary changes. I just wanted to give a small example to give you an idea of what you can do. Let me know what you think about it and if you have any ideas for some code scripts that you would like to see.

If you enjoy using Twitter, you may want to take a look at our Twitter Tag Cloud Service that we provide. You can also download the scripts to run the script from your own site.

Addendum (05/12/10):

This code has been added to the BrownPHP Code Repository if you would like to download it. You can go straight to the download by clicking the BrownPHP Twitter Class


Twitter Tag Cloud Service

The past few months I have been using Twitter a lot.  I’m trying to find and follow people that are heavily involved with PHP or at least providing knowledgeable Tweets about PHP.  I guess you could say that I am always open to new networking opportunities within the PHP community. :)

If you don’t know what Twitter is, well then you aren’t keeping up with your current events very well.  I’m not going to use this post explaining what it is more than a mini-blogging service.  If you don’t know, check their site out.  I provided a link above.

The problem I was having was actually judging whether or not it would be worth my time following their tweets. I mean, I follow someone that is really respected in a certain field, only to find out that they tweet about their new puppy they just got more than anything. So I made a PHP script that generates a “Twitter Tag Cloud.”  Using this, you can generate your own Twitter tag cloud to put on your personal site to display the top keywords that you have been tweeting about.

I also provide a REST service so that you don’t have to upload or play with any code yourself other than accessing the BrownPHP Twitter Tag Cloud service. I’ll show you more about that later. Or you can just use the textbox I have here to check out your own Twitter Cloud or a friends.

This is helpful to judge what the person tweets about the most.  The more the Twitter-er Tweets about a certain keyword, the larger it appears. Like if you would look at mine.  One of the large keywords is “informative.”  I also run a site called, Informative Post, and I tweet about it often, so it makes sense that this would be the largest keyword.

View Your Twitter Tag Cloud Now

Try it yourself! Type in a twitter username to see what that twitter-er tweets about more often:


Pretty cool, huh?  At the bottom of this, I provide a link where you can download the PHP files from.  I am also providing this as a REST service.

REST Twitter Tag Cloud Service

Parameters:

  • user (required)
    • Twitter Username
  • history (optional)
    • Number of past status updates to look for
    • Defaults to 100
    • Max is 200
  • size (optional)
    • Number of Keywords to return
    • Defaults to 50
    • Max is 75

Example REST call to get my (dlbrown06) Twitter tag cloud:
http://www.brownphp.com/rest/twittertagcloud.php?user=dlbrown06
Response:

<div id="twitterTagCloud"><a class="size2 tag" title="dlbrown06's Tweets Covering money" rel="nofollow" href="http://search.twitter.com/search?q=+money+from%3Adlbrown06" target="_blank">money</a>
<a class="size2 tag" title="dlbrown06's Tweets Covering taking" rel="nofollow" href="http://search.twitter.com/search?q=+taking+from%3Adlbrown06" target="_blank">taking</a>
<a class="size1 tag" title="dlbrown06's Tweets Covering networking" rel="nofollow" href="http://search.twitter.com/search?q=+networking+from%3Adlbrown06" target="_blank">networking</a>
<a class="size1 tag" title="dlbrown06's Tweets Covering social" rel="nofollow" href="http://search.twitter.com/search?q=+social+from%3Adlbrown06" target="_blank">social</a>
<a class="size2 tag" title="dlbrown06's Tweets Covering would" rel="nofollow" href="http://search.twitter.com/search?q=+would+from%3Adlbrown06" target="_blank">would</a>
<a class="size2 tag" title="dlbrown06's Tweets Covering children" rel="nofollow" href="http://search.twitter.com/search?q=+children+from%3Adlbrown06" target="_blank">children</a>
<a class="size2 tag" title="dlbrown06's Tweets Covering account" rel="nofollow" href="http://search.twitter.com/search?q=+account+from%3Adlbrown06" target="_blank">account</a>
<a class="size4 tag" title="dlbrown06's Tweets Covering working" rel="nofollow" href="http://search.twitter.com/search?q=+working+from%3Adlbrown06" target="_blank">working</a>
<a class="size3 tag" title="dlbrown06's Tweets Covering article" rel="nofollow" href="http://search.twitter.com/search?q=+article+from%3Adlbrown06" target="_blank">article</a>
<a class="size2 tag" title="dlbrown06's Tweets Covering start" rel="nofollow" href="http://search.twitter.com/search?q=+start+from%3Adlbrown06" target="_blank">start</a>
<a class="size2 tag" title="dlbrown06's Tweets Covering currently" rel="nofollow" href="http://search.twitter.com/search?q=+currently+from%3Adlbrown06" target="_blank">currently</a>
<a class="size1 tag" title="dlbrown06's Tweets Covering making" rel="nofollow" href="http://search.twitter.com/search?q=+making+from%3Adlbrown06" target="_blank">making</a>
<a class="size4 tag" title="dlbrown06's Tweets Covering online" rel="nofollow" href="http://search.twitter.com/search?q=+online+from%3Adlbrown06" target="_blank">online</a>
<a class="size2 tag" title="dlbrown06's Tweets Covering twitter" rel="nofollow" href="http://search.twitter.com/search?q=+twitter+from%3Adlbrown06" target="_blank">twitter</a>
<a class="size2 tag" title="dlbrown06's Tweets Covering morning" rel="nofollow" href="http://search.twitter.com/search?q=+morning+from%3Adlbrown06" target="_blank">morning</a>
<a class="size1 tag" title="dlbrown06's Tweets Covering sites" rel="nofollow" href="http://search.twitter.com/search?q=+sites+from%3Adlbrown06" target="_blank">sites</a>
<a class="size2 tag" title="dlbrown06's Tweets Covering right" rel="nofollow" href="http://search.twitter.com/search?q=+right+from%3Adlbrown06" target="_blank">right</a>
<a class="size5 tag" title="dlbrown06's Tweets Covering informative" rel="nofollow" href="http://search.twitter.com/search?q=+informative+from%3Adlbrown06" target="_blank">informative</a>
<a class="size2 tag" title="dlbrown06's Tweets Covering hosting" rel="nofollow" href="http://search.twitter.com/search?q=+hosting+from%3Adlbrown06" target="_blank">hosting</a>
<a class="size1 tag" title="dlbrown06's Tweets Covering until" rel="nofollow" href="http://search.twitter.com/search?q=+until+from%3Adlbrown06" target="_blank">until</a>
<a class="size2 tag" title="dlbrown06's Tweets Covering people" rel="nofollow" href="http://search.twitter.com/search?q=+people+from%3Adlbrown06" target="_blank">people</a>
<a class="size1 tag" title="dlbrown06's Tweets Covering looking" rel="nofollow" href="http://search.twitter.com/search?q=+looking+from%3Adlbrown06" target="_blank">looking</a>
<a class="size2 tag" title="dlbrown06's Tweets Covering starting" rel="nofollow" href="http://search.twitter.com/search?q=+starting+from%3Adlbrown06" target="_blank">starting</a>
<a class="size2 tag" title="dlbrown06's Tweets Covering really" rel="nofollow" href="http://search.twitter.com/search?q=+really+from%3Adlbrown06" target="_blank">really</a>
<a class="size3 tag" title="dlbrown06's Tweets Covering diablo" rel="nofollow" href="http://search.twitter.com/search?q=+diablo+from%3Adlbrown06" target="_blank">diablo</a></div>

Then you can use CSS to format the cloud however you like!

Download the Twitter Tag Cloud Source Code:

If you would like to use this source code yourself, you are free to do so.  All I ask is that you provide a link to this site, or somehow recognize the work that I put into it somehow.  I would love to see what kind of mashups that the PHP community could do with this source code. Enjoy!

Addendum (05/12/10):

This code has been added to the BrownPHP Code Repository if you would like to download it. You can go straight to the download by clicking the BrownPHP Twitter Class


  • BrownPHP Tag Cloud

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

    Powered by Yahoo! Answers