Tuesday, September 24, 2013

Ask Google for your flights, reservations, package delivery info, and more!

http://productforums.google.com/forum/#!category-topic/websearch/how-do-iusing-google-search/8b2OyQJIyCg

Sunday, September 15, 2013

Magento how to get all parent categories

Hi.. Guys today i have learned something very interesting, which is HOW TO GET (ONLY) PARENT CATEGORIES from the category tree.

[php]
$categories = Mage::getModel('catalog/category')->getCollection()
->addAttributeToSelect('*')//or you can just add some attributes
->addAttributeToFilter('level', 2)//2 is actually the first level
->addAttributeToFilter('is_active', 1);//if you want only active categories
foreach ($categories as $_category){
echo $_category->getName().'<>';
}
[/php]

Hope it helps someone!!

Happy coding :-)

Saturday, September 7, 2013

PHP : Get redirect URL using cURL

Trick for retrieving the redirect URL using PHP cURL :
Ever got irritated when you trying loading a URL with PHP cURL function and its actually redirecting you to a new url and you programmatically want to find the actual URL or redirected URL so here is the function you are looking for:

[php]
function getActualURL($url){
$ch = curl_init();
$timeout = 0;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
// Getting binary data
$header = curl_exec($ch);
$retVal = array();
$fields = explode("\r\n", preg_replace('/\x0D\x0A[\x09\x20]+/', ' ', $header));
foreach( $fields as $field ) {
if( preg_match('/([^:]+): (.+)/m', $field, $match) ) {
$match[1] = preg_replace('/(?<=^|[\x09\x20\x2D])./e', 'strtoupper("\0")', strtolower(trim($match[1])));
if( isset($retVal[$match[1]]) ) {
$retVal[$match[1]] = array($retVal[$match[1]], $match[2]);
} else {
$retVal[$match[1]] = trim($match[2]);
}
}
}
//here is the header info parsed out
echo '<pre>';
print_r($retVal);
echo '</pre>';
//here is the redirect
if (isset($retVal['Location'])){
echo $retVal['Location'];
} else {
//keep in mind that if it is a direct link to the image the location header will be missing
echo $_GET[$urlKey];
}
curl_close($ch);
}
[/php]

Hope someone will find this helpfull,
Happy coding :-)

Thursday, September 5, 2013

PHP : Very Usefull Special Character Definitions

Collection of PHP Regular expression keywords which you often get lost from your mind:

When we come to use regular expression for performing pretty task on string or validations this is common situation that we all forget or often confused with keywords to use thats why i just found this will be helpful to post it here :

Special Character Definitions
[php]
\ Quote the next metacharacter
^ Match the beginning of the line
. Match any character (except newline)
$ Match the end of the line (or before newline at the end)
| Alternation
() Grouping
[] Character class
* Match 0 or more times
+ Match 1 or more times
? Match 1 or 0 times
{n} Match exactly n times
{n,} Match at least n times
{n,m} Match at least n but not more than m times
More Special Character Stuff
\t tab (HT, TAB)
\n newline (LF, NL)
\r return (CR)
\f form feed (FF)
\a alarm (bell) (BEL)
\e escape (think troff) (ESC)
\033 octal char (think of a PDP-11)
\x1B hex char
\c[ control char
\l lowercase next char (think vi)
\u uppercase next char (think vi)
\L lowercase till \E (think vi)
\U uppercase till \E (think vi)
\E end case modification (think vi)
\Q quote (disable) pattern metacharacters till \E
Even More Special Characters
\w Match a "word" character (alphanumeric plus "_")
\W Match a non-word character
\s Match a whitespace character
\S Match a non-whitespace character
\d Match a digit character
\D Match a non-digit character
\b Match a word boundary
\B Match a non-(word boundary)
\A Match only at beginning of string
\Z Match only at end of string, or before newline at the end
\z Match only at end of string
\G Match only where previous m//g left off (works only with /g)
[/php]

For more read through this link : Regexp Introduction