02 May, 2010

Check If A Value Exists In Multidimensional Array

Here is a function which will help you to know if a value exists in Multidimensional Array.

  1. function in_mdarray($needle,$array)
  2. {
  3.     if (array_key_exists($needle,$array) or in_array($needle,$array))
  4.     {
  5.          return true;
  6.     }
  7.     else
  8.     {
  9.           $return = false;
  10.           foreach (array_values($array) as $value)
  11.           {
  12.               if (is_array($value) and !$return)
  13.               {
  14.                   $return = in_mdarray($needle,$value);
  15.               }
  16.           }
  17.           return $return;
  18.     }
  19. }

01 May, 2010

Check If A Value Exists In Associative Array

The "in_array" function of php checks if a value exists in an array. But it don't work properly with associative array. Here is a function which will work properly with associative array.

  1. function in_assoc($needle,$array)
  2. {
  3.     $key = array_keys($array);
  4.     $value = array_values($array);
  5.     if (in_array($needle,$key)){return true;}
  6.     elseif (in_array($needle,$value)){return true;}
  7.     else {return false;}
  8. }

23 March, 2010

Grab All Links Of Web Page Using Python

I always try to make useful program to learn a Programming Language. Now I am trying to learn Python. I will try to post my practice code here. Today I will show you how to grab all links of web page using Python's "BeautifulSoup" module.

  1. import urllib,BeautifulSoup
  2. def get_links(url):
  3.      page = urllib.urlopen(url)
  4.      soup = BeautifulSoup.BeautifulSoup(page.read())
  5.      aTag = soup.findAll("a")
  6.      link = []
  7.      for a in aTag:
  8.           if a.has_key("href"):
  9.                link.append(a['href'])
  10.      return link
  11. print get_links("http://localhost/PRACTIECE/a2/")
Enjoy!!!

23 February, 2010

Replace Links With Html

You can replace links with html tag using PHP regular expression.
Here is an example of it.
  1. function replace_link_with_link($string)
  2. {
  3.     $pattern = "|http://(\w+)([A-Z0-9-_:./?]+)*|i";
  4.     $string = preg_replace($pattern,'<a href="\0">\0</a>',$string);
  5.     return $string;
  6. }
Enjoy. Help Helpless.