PHP
How to change date formate of twitter field “created_at”?
php coder who is reading twitter API for latest tweets want to change date/ timestamp format which is returned by ‘created_at’ key. Most of the times I use JSON based request. e. i. http://search.twitter.com/search.json?q=phpmind
These are the fields returned by above request.
Hope fully you understand how json string looks like.

Now it is time to convert date time stamp in more readable format.
Please change the date parameters if you want to play with different date formats.
If you want to use javaScript way to format dates here is very cool function.
Returned by twitter – Wed, 09 Feb 2011 02:44:36 +0000
Returned by this javaScript function, TwitterDateConverter ($object->returns[0]->created_at);
Output will be for Example “14 minutes ago”
See Image Below!
<script type="text/JavaScript">
function TwitterDateConverter(time){
var date = new Date(time),
diff = (((new Date()).getTime() - date.getTime()) / 1000),
day_diff = Math.floor(diff / 86400);
if ( isNaN(day_diff) || day_diff < 0 || day_diff >= 31 )
return;
return day_diff == 0 && (
diff < 60 && "just now" ||
diff < 120 && "1 minute ago" ||
diff < 3600 && Math.floor( diff / 60 ) + " minutes ago" ||
diff < 7200 && "1 hour ago" ||
diff < 86400 && Math.floor( diff / 3600 ) + " hours ago") ||
day_diff == 1 && "Yesterday" ||
day_diff < 7 && day_diff + " days ago" ||
day_diff < 31 && Math.ceil( day_diff / 7 ) + " weeks ago";
}
</script> |

How to Enable CURL in WAMP?
CURL is not enabled by default in WAMP. Every php programmer have to use CURL to make remote connection.
The steps are as follows :
1) Close WAMP (if running)
2) Navigate to WAMP\bin\php\(your version of php)\
3) edit php.ini
4) Search for curl, uncomment extension=php_curl.dll
5) Navigate to WAMP\bin\Apache\(your version of apache)\bin\
6) edit php.ini
7) Search for curl, uncomment extension=php_curl.dll
8 ) Save both
9) Restart WAMP
That will solve your Curl issues.
Enjoy!
How to prevent XSS attacks through php?
There are a number of ways hackers put to use for XSS attacks, PHP’s built-in functions do not respond to all sorts of XSS attacks. Hence, functions such as strip_tags, filter_var, mysql_real_escape_string, htmlentities, htmlspecialchars, etc do not protect us 100%. You need a better mechanism, here is what is solution:
<?php function xss_clean($data) { // Fix &entity\n; $data = str_replace(array('&','<','>'), array('&amp;','&lt;','&gt;'), $data); $data = preg_replace('/(&#*\w+)[\x00-\x20]+;/u', '$1;', $data); $data = preg_replace('/(&#x*[0-9A-F]+);*/iu', '$1;', $data); $data = html_entity_decode($data, ENT_COMPAT, 'UTF-8'); // Remove any attribute starting with "on" or xmlns $data = preg_replace('#(<[^>]+?[\x00-\x20"\'])(?:on|xmlns)[^>]*+>#iu', '$1>', $data); // Remove javascript: and vbscript: protocols $data = preg_replace('#([a-z]*)[\x00-\x20]*=[\x00-\x20]*([`\'"]*)[\x00-\x20]*j[\x00-\x20]*a[\x00-\x20]*v[\x00-\x20]*a[\x00-\x20]*s[\x00-\x20]*c[\x00-\x20]*r[\x00-\x20]*i[\x00-\x20]*p[\x00-\x20]*t[\x00-\x20]*:#iu', '$1=$2nojavascript...', $data); $data = preg_replace('#([a-z]*)[\x00-\x20]*=([\'"]*)[\x00-\x20]*v[\x00-\x20]*b[\x00-\x20]*s[\x00-\x20]*c[\x00-\x20]*r[\x00-\x20]*i[\x00-\x20]*p[\x00-\x20]*t[\x00-\x20]*:#iu', '$1=$2novbscript...', $data); $data = preg_replace('#([a-z]*)[\x00-\x20]*=([\'"]*)[\x00-\x20]*-moz-binding[\x00-\x20]*:#u', '$1=$2nomozbinding...', $data); // Only works in IE: <span style="width: expression(alert('Ping!'));"></span> $data = preg_replace('#(<[^>]+?)style[\x00-\x20]*=[\x00-\x20]*[`\'"]*.*?expression[\x00-\x20]*\([^>]*+>#i', '$1>', $data); $data = preg_replace('#(<[^>]+?)style[\x00-\x20]*=[\x00-\x20]*[`\'"]*.*?behaviour[\x00-\x20]*\([^>]*+>#i', '$1>', $data); $data = preg_replace('#(<[^>]+?)style[\x00-\x20]*=[\x00-\x20]*[`\'"]*.*?s[\x00-\x20]*c[\x00-\x20]*r[\x00-\x20]*i[\x00-\x20]*p[\x00-\x20]*t[\x00-\x20]*:*[^>]*+>#iu', '$1>', $data); // Remove namespaced elements (we do not need them) $data = preg_replace('#</*\w+:\w[^>]*+>#i', '', $data); do { // Remove really unwanted tags $old_data = $data; $data = preg_replace('#</*(?:applet|b(?:ase|gsound|link)|embed|frame(?:set)?|i(?:frame|layer)|l(?:ayer|ink)|meta|object|s(?:cript|tyle)|title|xml)[^>]*+>#i', '', $data); } while ($old_data !== $data); // we are done... return $data; } ?> |
How to add www in url through .htaccess?
Recently I was working on a project and SEO team requested to add WWW forcefully in project URL. I asked why, they said.
“Must have www in your URL! Google Like it.”
So here is .htaccess snippet to force a url with WWW
When you will type http://phpmind.com it will automatically become http://www.phpmind.com
![]()
RewriteEngine on rewritecond %{http_host} ^phpmind.com [nc] rewriterule ^(.*)$ http://www.phpmind.com/$1 [r=301,nc] |
This is how I did in this site. I am telling you one .htaccess secret. You can experience here only.
How to add and disable ETags through .htaccess?
What is ETag?
An ETag, or entity tag, is part of HTTP and it provides a unique identifier for the resource being supplied.
This identifier can then be used to validate if it resource has been modified or
Browser requests can use an etag and an if-match header to check whether a resource has been modified since it was served, providing an efficient caching facility that relies on content rather than on timeouts.
Use this code to setup ETags on your server, using following htaccess.
FileETag MTime Size <ifmodule mod_expires.c> <filesmatch "\.(jpg|gif|png|css|js)$"> ExpiresActive on ExpiresDefault "access plus 1 year" </filesmatch> </ifmodule> |
Sometimes developer want to be disable ETags from headers. To disable ETags, make sure to include following snippet in your .htaccess file.

How to use .htaccess for permanent redirect 301?
If you are making search engine friendly URL from pages which are using query string (i.e. www.phpmind.com?id=2 ) and pages which are indexed in search engines or if you are rewriting your website with new urls then you need permanent redirect 301!!
Let me tell you what happens here. If you remove a page which is already in Google it will generate error and that is not good for your site.
So .htaccess for permanent redirect will save life! Just redirect old to new url and Google will like your site this is good practice in terms of SEO too.
This one line PHP code is worth to add in .htaccess file of your root directory without loosing search engine ranking in Google and Others.
Redirect 301 /oldPage.html http://mydomain.com/newpage.html Another Way redirectpermanent /contact-me.html http://www.phpmind.com/contact-us.html |
How to prevent hot linking using .htaccess?
Don’t let your users your photo steel! If you have very good photo or even not good! and want to prevent your files using php technology, such as jpg, gif, bmp, png etc.
You can just put this code in root directory of .htaccess file of your php project and it won’t let others site use you files directly and it will save your bandwidth. This is the main purpose of blocking hotlinking using .htaccess in php.
RewriteEngine On RewriteCond %{HTTP_REFERER} !^http://(.+\.)?my-website-url\.com/ [NC] RewriteCond %{HTTP_REFERER} !^$ RewriteRule .*\.(jpg|gif|bmp|png)$ http://phpmind.com/no-hotlinking-please.png [L] |
How to upload big file using .htaccess?
You can upload big file yes it is possible without applet and other third party lib!
Simply put these standard configuration values and they will overwrite php.ini setting, which does not let you upload big file in your server.
php_value upload_max_filesize 10M php_value post_max_size 10M php_value max_execution_time 300 php_value max_input_time 300 |
How to force http to https through .htaccess?
In order to force https:// in payment and other secure pages even if your customer try to access http:// or they forget. You can use .htaccess file and simple code.
Example –
I had created a page http://www.phpmind.com/blog/
I want my visitor to access https://www.phpmind.com/blog/
Here is code which will force them to redirect to https://
RewriteEngine On RewriteCond %{HTTPS} off RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} |
How to explode a string and create hyperlink?
Here is very easy code to create Tagged List with hyperlink.
I have found several events where we have to create dynamic URL with Tagged text.
I have used very simple approach. Just Exploded sting and replaced space with “-”
You can use PHP function preg_replace() or str_replace() then used here-docs for creating hyperlink.
Instead of Echo Hear Docs is much better solution.
And at last removed the last Comma, and return your comma separated Dynamic links.
$tags = "jQuery Tutorials, PHP Display Errors, Drupal Module"; $tagL=""; function b2b_article_LinkedTags($stringTags) { $expTags = explode(",",$stringTags); foreach($expTags as $key=>$Tagevalue) { $Tagevalue = trim($Tagevalue); $link = preg_replace('/[^-a-z0-9]/', '-', strtolower($Tagevalue) ); //$link = str_replace(" ","-", strtolower($Tagevalue)); $tagL.=<<<EOF <a href="./articles-tagged-$link">$Tagevalue</a>, EOF; } return substr($tagL, 0, -1); } echo b2b_article_LinkedTags($tags) |

