Useful php snippet for web designers

I’d like to share some useful php snippets I often use while developing my websites. I just save them in my snippet folder in Dreamweaver.

  1. Quick variable retrieving (string):
    $page= $_GET["page"];
      if (!$page)   $page = "page name here";
  2. Quick variable retrieving (number):
    $id= (int)$_GET["id"];
      if (!$id)   $id = 1;
  3. A ‘for’ cycle:
    for($i=1950; $i<=2007; $i++) {
    echo "$i";
    }
  4. A ‘switch’ statement:
    switch ($lang){
                case "" :
    	      echo "";
    	      break;
    	}
  5. Antispam email:

    <?php
    function InsertMail($mail)
        {
        if ($mail=='') return '';
        $mail = str_replace(array('@',':','.'), array('&amp;#064;','&amp;#058;','&amp;#046;'), $mail);
        $mail = '<a href=mailto&amp;#058;'.$mail.'>'.$mail.'</a>';
        $len = strlen($mail);
        $i=0;
        while($i<$len)
            {
            $c = mt_rand(1,4);
            $par[] = (substr($mail, $i, $c));
            $i += $c;
            }
        $join = implode('"+ "', $par);
    
        return '<script type="text/javascript" language="javascript">
        <!--
        document.write("'.$join.'")
        //-->
        </script>';
        }
    echo InsertMail ('user@example.com');
    ?>
    
  6. Replace accents in a query results
    function replace_accents($str) {
    $str = htmlentities($str, ENT_COMPAT, "UTF-8");
    $str = preg_replace('/&amp;amp;amp;amp;amp;([a-zA-Z])(uml|acute|grave|circ|tilde);/','$1',$str);
    return html_entity_decode($str);}
    
This entry was posted in copy&paste, php, web design and tagged , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>