satishgaudo.com

Understanding technology

Archive for the 'PHP' Category

Php:trim a string without cutting the word

function neat_trim($str, $n, $delim=’…’) { $len = strlen($str); if ($len > $n) { preg_match(’/(.{’ . $n . ‘}.*?)\b/’, $str, $matches); return rtrim($matches[1]) . $delim; } else { return $str; [...]

19 November 2011 at 13:13 - Comments

Php:get valid url

function formatUrl($sDispTitle){ $sDispTitle = str_replace(’-',”,$sDispTitle); $sDispTitle = str_replace(’ ‘,’-',$sDispTitle); $sDispTitle = str_replace(’,',’-',$sDispTitle); $sDispTitle = str_replace(’&’,”,$sDispTitle); $sDispTitle = str_replace(’/',’-',$sDispTitle); $sDispTitle = str_replace(’.',”,$sDispTitle); $sDispTitle = str_replace(’?',”,$sDispTitle); $sDispTitle = str_replace("’",”,$sDispTitle); $sDispTitle = str_replace("!",”,$sDispTitle); $sDispTitle = str_replace(’"’,”,$sDispTitle); $sDispTitle = str_replace(’%',”,$sDispTitle); $sDispTitle = str_replace(’#',”,$sDispTitle); return $sDispTitle; }

19 November 2011 at 13:11 - Comments

PHP:check if valid email using regular expression

function isValidEmail($email){ return eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email); }

19 November 2011 at 13:08 - Comments

Regular expression to replace multiple white spaces with single -

$sTitle="test test123";   $sTitle = preg_replace("’\s+’", ‘-’, $sTitle);   print $sTitle;   output:test-test123

19 November 2011 at 12:04 - Comments

PHP: Uploaded media file type validation

function typevalidation($uploadfiletype,$mediatype,$tmpfilename="") { if ($tmpfilename) { $sfiletype = shell_exec("mimetype -bi $tmpfilename"); //echo "In".$sfiletype; $typearr=explode("/",$sfiletype); $filetype=$typearr[0]; $fileext=$typearr[1]; }   if($uploadfiletype==’thumb’) { if($filetype!=’image’) { $errmsg="errormessage1"; } else{ $errmsg="ok"; } } else if($uploadfiletype==’media’) { if($mediatype=="image") { if($filetype!=’image’) { $errmsg="errormessage2"; } else {$errmsg="ok"; } } else if($mediatype=="video") { if($filetype!=’video’) { $errmsg="errormessage3"; } elseif(strpos($fileext,"flv")===false) { $errmsg="errormessage4"; } else {$errmsg="ok"; } } } return $errmsg; }

19 November 2011 at 11:57 - Comments

Php: Function to remove unwanted utf-8 chars

Function removes unwanted utf-8 characters except correct ASCII & UTF-8 characters (excluding 4-byte+ UTF-8 sequences).

function removeInvalidUtf8Chars($s) {   if(empty($s)) return $s; $s = preg_match_all("#[\x09\x0A\x0D\x20-\x7E]| [\xC2-\xDF][\x80-\xBF]| \xE0[\xA0-\xBF][\x80-\xBF]| [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}| \xED[\x80-\x9F][\x80-\xBF]#x", $s, $m ); return implode("",$m[0]);     return $s; }

24 August 2011 at 18:53 - Comments

View counter application:source code

Here is the Full source code for view counter application for counting the page views . Please use the example.txt for how to use the application. It uses memcache. Install the memcached daemon and php library to use it. It uses mysql 5 and php 5. http://satishgaudo.com/satblog/ext_img/view_counter.rar

31 May 2011 at 18:51 - Comments

Image resizer class: Resizes and saves image

<?php /* Purpose: Resizes and saves image Requires : Requires PHP5, GD library. Usage Example: include("classes/resize_class.php"); [...]

31 May 2011 at 18:26 - Comments

Regular expression: code to get all the anchor tags from the html code

Regular expresion used:’#&lt;a\s*(?:href=[\'"]([^\'"]+)[\'"])?\s*(?:title=[\'"]([^\'"]+)[\'"])?.*?&gt;((?:(?!&lt;/a&gt;).)*)&lt;/a&gt;#i’   Example: <code> &lt;?</code>   <code>$strcontent="&lt;table&gt;&lt;tr&gt;&lt;td&gt;&lt;a href=’http://satishgaudo.com’ &gt;satishgaudo.com&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;a href=’http://satishgaudo.com/1′ &gt;satishgaudo.com/1&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;";</code>   <code>preg_match_all(’#&lt;a\s*(?:href=[\'"]([^\'"]+)[\'"])?\s*(?:title=[\'"]([^\'"]+)[\'"])?.*?&gt;((?:(?!&lt;/a&gt;).)*)&lt;/a&gt;#i’,$strcontent, $aOutput);   print_r($aOutput);   </code>   <code>?&gt; </code> Output: <pre id="line1">Array ( [0] =&gt; Array ( [0] =&gt; &lt;<span>a</span><span> href</span><span>=’</span><a href="view-source:http://satishgaudo.com/">http://satishgaudo.com</a><span>’ </span>&gt;satishgaudo.com&lt;/<span>a</span>&gt; [1] =&gt; &lt;<span>a</span><span> href</span><span>=’</span><a [...]

29 April 2011 at 16:30 - Comments
Brooks Leaming at 06:34 on 3 May 2011
I believe the author that we want to share the knowledge we gain!

PHP function to close html tags if not closed properly

/** * closetags * used to close html tags incase not closed properly * @param string $html – html string * @access public */   function closetags($html){ $arr_single_tags = array(’meta’,'img’,'br’,'link’,'area’); preg_match_all(’#&lt;([a-z]+)(?: .*)?(?&lt;![/|/ ])\s*&gt;#iU’, $html, $result); $openedtags = $result[1]; preg_match_all(’#&lt;/([a-z]+)&gt;#iU’, $html, $result); $closedtags = $result[1]; $len_opened = count($openedtags); if (count($closedtags) == $len_opened){ return $html; } $openedtags = array_reverse($openedtags); //re arrange open tags and closed tags for count $aOpenedtagsCnt=Array(); $aClosedtagsCnt=Array(); if(is_array($openedtags)){ foreach($openedtags as $iK =&gt;$sTag){ if(!isset($aOpenedtagsCnt[$sTag])){ $aOpenedtagsCnt[$sTag]=1; }else{ $aOpenedtagsCnt[$sTag]++; } } } if(is_array($closedtags)){ foreach($closedtags as $iK =&gt;$sTag){ if(!isset($aClosedtagsCnt[$sTag])){ $aClosedtagsCnt[$sTag]=1; }else{ $aClosedtagsCnt[$sTag]++; } } } for ($i=0; [...]

24 September 2010 at 15:41 - Comments
jimmi at 18:07 on 29 September 2010
Nice function. Thanks
Private Krankenversicherung at 23:47 on 1 October 2010
the useful thoughts you provided do help our team's research for our group, thanks. - Lucas