PHP convert timezones

Hi Guys, Just a quick tip here. There are times when you would like to convert a Timezone (say America/New_York) to another Timezone (say UTC) With classes such as DateTime and DateTimeZone available from php 5.2+ this task has become piece of cake. Let me show you how its done. In our example we will be converting America/New_York to UTC time Good thing is that Day light savings are taken into consideration automatically. Code snippet [crayon-5885be4a7d63f134479617/] This is it you can create a function for the above code in your Util class as shown below and get some conversions happening.  Generic Function [crayon-5885be4a7d64f794967571/] Usage [crayon-5885be4a7d655660691534/]   Just in case you would like to add to above solution please leave you comments.   I hope this helps     … [Read more...]

PHP Find pages Indexed by Bing for a domain example

Hi Guys, In the previous post I showed you how to get pages indexed by Google. We used function file_get_contents() to get page as HTML and then parse it to extract the value that we are interested in.   This post is pretty much the same logic with only difference that we will be using CURL to get the contents and the parse it Pages indexed by Bing can be quickly checked with query site:yoursite.com and the Number that we are interested in is directly under the search box with value something like this "1231123 Results" This post shows you how to extract that value. The PHP Code snippet Here is a function that you can include in your utility class or just [ltu] [crayon-5885be4a7e40e763563574/]   [/ltu] Usage Using the above function as per the previous post is quite save and is shown below [crayon-5885be4a7e41c969301520/] Bing does not have any API as such that can give you this value. So this is kinda workaround. If I am wrong to say this please correct me. I hope that this helps Cheers. … [Read more...]

PHP Find how many pages are Indexed by Google

Hi Guys, Lately I have been giving undivided attention to my new born daughter so for writing lengthy how-tos, I am finding less time for. But that doesn't mean that I am unable to share some quick how tos. You see, that in many Browser Extensions and/or toolbars that give information on SEO stuff they list page indexed by various search engines such as Yahoo, Bing and Google. An example is shown below. This post shows you how to get that value from Google. The PHP Code snippet Here is a function that you can include in your utility class or just [ltu] [crayon-5885be4a7e8e4946996764/] Just in case above doesn't work then you can try out a curl request such as the one shown below, you may need to check if google returns a "document moved" status. In that case you will have to copy whatever Google suggest to make it work. [crayon-5885be4a7e8f1513619475/] [/ltu] Usage Using the above function is quite simple and is shown below [crayon-5885be4a7e8f8675695458/] This is not very pretty but there is no standard API that I am aware of doing the same. In post to come we will be looking  "Find how many pages are Indexed by Bing using PHP" Idea is the same but regular expression will be different. I hope that this helps Cheers. … [Read more...]

PHP Find replace URL in text to HTML Links

Hi Guys, This is kinda straight forward thing to do using Regex magic so let go ahead and build our function which we will call plainUrlToLink() Here is that snippet that you can just copy paste. Don't forget to give this post a +1 if it helped you. The Snippet [crayon-5885be4a7f276256529436/] As you notice that I am not doing a case insensitive replacing that's for a reason. That's that for our function but now lets check how to use it. [ltu] Usage To use this function is simple. It will return a plain text content if [crayon-5885be4a7f281137442194/] Above will print [crayon-5885be4a7f288785566536/] Please note that this function is good for finding links in plain text only and will not work if you pass HTML to it. Also if you want to include FTP URL detection then change you regex to this [crayon-5885be4a7f28e160971947/] I hope this helps. Every code has room for improvement, if you think this snippet can be improved please leave your feedback in comments, that will help other readers too. Cheers [/ltu] … [Read more...]

How to Lighten or Darken HEX or RGB color in PHP and JavaScript

Hi Guys, Just a thought and following up on my previous post is a function that you can use in your PHP code to brighten or darken a HEX or a RGB color, For color manipulation in JavaScript you can look at this cool library https://github.com/jfsiii/chromath PHP Function Below is the function you can use to make on the fly adjustment to colors [crayon-5885be4a7fe9b296937757/] For sure above function can have less lines but for simplicity and readability I think putting it as above makes sense. Usage - Lightning Colors [crayon-5885be4a7fea7255403914/] Usage - Darkening Colors [crayon-5885be4a7feae725663588/]   Now lets check the JavaScript implementation of this function. JavaScript Equivalent Function For Lightening and Darkening HEX or RGB Colors You can definitely do better job with shift operators but I always think of readability of code first. The code below can be shrunk but at least you get the Idea what's happening in a typical way [crayon-5885be4a7feb5755616349/] Usage 1 - Lighten [crayon-5885be4a7febe526321700/] Now lets check how we can use the same function to Darken our Hex or RBG colors Usage 2 - Darken [crayon-5885be4a7fec4211263161/] I hope this helps. Got a better implementation of these function. I would love to know and this will help my readers too. Share it, leave your comments Cheers   … [Read more...]

PHP Convert HEX color code to RGB or RGB to HEX

Hi Guys, This is a quick TIP so that I don't have a burden of not sharing this age old function I wrote ages ago with you :) Anyway there are many occasion when you would like to problematically adjust say brightness, red, green, blue depths for a color code, Let me show you how to do it in PHP PHP Function [crayon-5885be4a808d6935079659/] Above is a simple to use method which you can use and modify as per your needs. Let me show you some example usage for this function   Usage 1 - Converting RGB to HEX This example shows how to use the above function to convert our rgb() values to a HEX code [crayon-5885be4a808e2525460590/] Above will print #CC8800   Usage 2 - Converting HEX code to RGB This example usage shows how to utilize above function to convert a HEXadecimal color code to rgb() values [crayon-5885be4a808e8790047484/] Will print [crayon-5885be4a808ee987200340/] As you've got the RGB values you can adjust them to suit your purpose. You can further shrink this function by atleast 3 more lines. I hope this helps Cheers       … [Read more...]

Building a php based image to data url (base64) function tool for img src attribute

Hi Guys, There are times when you would want to save HTTP requests to your servers. One thing you can do is to "include" your image in the html file itself. Sure it will make your HTML file a bit heavier but the client won't be hitting your server for that extra call to load the image. Other reason why you would want to include your image as base64 within the HTML file itself is, just if you want to protect the folder structure on your website (people won't know where your images are stored). You can also use a technique called CSS sprites which you can read more about from this brief tutorial on w3schools. This will result in loading a single image just once and most browsers will cache it, thereon you can use CSS to manipulate your image positioning. Here is what I would use with PHP 5.3 Using fileinfo [crayon-5885be4a80e09465676288/] and for older version of PHP using deprecated function mime_content_type we can rewrite the above code as Using mime_content_type [crayon-5885be4a80e14127442259/] fileinfo functions/class methods are based on magic mime database. sometime you will get an error where it fails to load the magic database. fileinfo extension need to be enabled to use fileinfo functions. Make sure that you have it enabled as shown below. (I am on windows box thats why you see .dll extension) [crayon-5885be4a80e1b929510707/] I hope that this little tip could benefit you. If you see any problem with listed code above please leave your comments. Also you can ask questions regarding what's going on above. If I can't answer then there must be someone who can. Cheers,   … [Read more...]

plupload and RackSpace cloud files

Hi Guys, There are a few uploaders that one can like. Mine favourite is plupload and there are heaps of reason behind that, but this post is not about discussing how good plupload is rather we will concentrate on how we can use plupload to upload files to your server and then send a signal to your server to upload that file to RackSpace cloud files. This post assume that average file size to be uploaded is not more than few 100 kilobytes else a bit more sophisticated error handling will be required so that files are reliably sent to RackSpace Cloud. You should know we will need to upload a file to the server hosting plupload and then upload it to RackSpace cloud. Doing it this way does have advantages as it offers greater flexibility. Here is an activity diagram of what we are going to do. This is just a basic activity diagram explaining core steps only. You can have better error handling. Above is just a skeleton activity diagram. In the code provided the upload failure to RS Cloud is not implemented and I leave it to you to do that. Lets get started. Create a folder pltest under your htdocs or html folder. This is the location where your web server find files to return to the user when queried. On linux path could be /var/www/html and on windows path could be something like this depending on where you've installed Apache. C:\Apache2\htdocs Download plupload First step is to download plupload and this can be done by visiting their website and follow the download instructions. Here is the link http://www.plupload.com/ Extract downloaded plupload in pltest folder. Getting Cloud Files PHP API Next we will need to download Cloud Files PHP API which you can find here https://github.com/rackspace/php-cloudfiles Extract PHP files in archive to to our pltest folder. Your folder structure should look like this now. Also make sure that you extract share folder under pltest. Sorry that is not part of the screenshot … [Read more...]

ExtJS 4 changing Grid row background color

Hello, ExtJS's grid system is really powerful module that for me is backbone of their framework. In this post I am going to show you a couple of methods you can use to change background colors of some random Grid rows. First up is to define couple of CSS classes which we will make use of later in our code. The CSS [geshi lang="css" nums="1" target="_self" ] [crayon-5885be4a81e9a662549284/] Now that we have our CSS classes to play with and apply to our Grid rows, let me take you through a full example from creating a new simple Grid to applying styles. The Markup [crayon-5885be4a81ea5252276628/] The Grid [crayon-5885be4a81eab696397320/] We our Grid is ready and properly rendered on our page. From here we can do few things. If we already know which rows to highlight that would be easy to do by using viewConfig property as shown below [crayon-5885be4a81eb3667399232/] Probably its a good thing that you override .x-grid-cell class definition and remove background-color property from it. In the above code I am just putting my custom classes for odd and even rows. But you can see that I have access to record object, thus I can apply conditions based on the data on hand. This is OK but what if you grid data is changed every 10 seconds and you are not sure which row to highlight with color red for example. I will do that sort of handling on store.load event and will do something like this [crayon-5885be4a81eb9831293408/] We are doing a check on every store reload and changing the background color according to the new data. So just to simulate consider adding this code to test it [crayon-5885be4a81ec0679192086/] Above code will refresh our store after 3 seconds (once only). This method also give you granular control to stylize individual columns too.   Demo http://jaspreetchahal.org/examples/extjs-grid-row-background-color.html   This is just my working solution. If you can come up … [Read more...]