Converting Text into Clickable Link – PHP

preg_replace - Searches $subject for matches to $pattern and replaces them with $replacement.
By using the above preg_replace function I have created a simple function which will first find urls inside text and convert all into clickable links
function convert_links($text)
{
 $text = preg_replace('#(script|about|applet|activex|chrome):#is', "\1:", $text);
 $ret = ' ' . $text;
 $ret = preg_replace("#(^|[n ])([w]+?://[w#$%&~/.-;:=,?@[]+]*)#is", "\1<a href="\2" target="_blank">\2</a>", $ret);
 $ret = preg_replace("#(^|[n ])((www|ftp).[w#$%&~/.-;:=,?@[]+]*)#is", "\1<a href="http://\2" target="_blank">\2</a>", $ret);
 $ret = preg_replace("#(^|[n ])([a-z0-9&-_.]+?)@([w-]+.([w-.]+.)*[w]+)#i", "\1<a href="mailto:\2@\3">\2@\3</a>", $ret);
 $ret = substr($ret, 1);
 preg_match_all("/<a href="(.+?)"/", $ret, $match);
 $result1 = array_unique($match);
 $count = count($result1[0]);
 if($count > 0)
 {
 foreach ($result1 as $val)
 {
 foreach ($val as $item)
 {
 $item = str_replace('<a href="', '', $item);
 $item = str_replace('"', '', $item);
 }
 }
 }
 return $ret;
}

Usage

echo convert_links('your text here');


Top 5 Free PHP & Mysql Cheat Sheets

The AddedBytes cheat sheet above covers multiple php topics such as functions list and regular expressions.
If you would rather not download a php cheat sheet, BlueShoes has provided an online php cheat sheet available for you to access at anytime.
Above you will find a helpful and quick online guide to basic and slightly complex MySQL commands.
If you’re looking for a full fledged MySQL cheat sheet, look no further. The cheat sheet above (again, by AddedBytes) contains MySQL native functions, php MySQL functions, MySQL data types, and MySQL sample queries.
The WikiBook MySQL Cheat Sheet is an open and member contributed online MySQL guide. It has the advantage of constantly being updated as the database languages evolves.

Validate IPv4 Address in PHP

Today I am going to tell how to validate IPv4 / IP address using PHP. Simple function which will used to check client IP address is valid or not.
Steps to validate IPv4 Address in PHP
  • Split IP address into segments  by dot(.)  using explode function
  • Make sure that there are 4 segments (eg : 192.168.1.45)
  • Make sure that IP cannot start with 0
  • IP segments must be digits & cannot be longer than 3 digits or greater than 255
function validate_ip($ip)
{
 //split ip address in to array by dot
 $ip_segments = explode('.', $ip);
 // Always 4 segments needed
 if (count($ip_segments) !== 4)
 {
  return FALSE;
 }
 // IP can not start with 0
 if ($ip_segments[0][0] == '0')
 {
  return FALSE;
 }
 // Check each segment
 foreach ($ip_segments as $segment)
 {
  // IP segments must be digits and can not be
  // longer than 3 digits or greater then 255
  if ($segment == '' OR preg_match("/[^0-9]/", $segment) OR $segment > 255 OR strlen($segment) > 3)
  {
   return FALSE;
  }
 }
 return TRUE;
}

//usuage
$ip = validate_ip("192.168.1.43");
if($ip)
{
  echo "Valid IP";
} else {
  echo "Invalid IP";
}


This function will return boolean true or false

Removing index.php from Codeigniter URL

If you are using Codeigniter you are noticed that by default index.php will be included with your URL.

But you can easily remove index.php from your CodeIgniter’s URL so that your URL should be like:

http://domain.com/about

To do this just follows the following steps:

Open config.php from system/application/config directory and replace

$config['index_page'] = “index.php” by $config['index_page'] = “”

Create a “.htaccess”file in the root of CodeIgniter directory (where the system directory resides), open the file using your favorite text editor, write down the following script and save it:

RewriteEngine on
RewriteCond $1 !^(index.php|resources|robots.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]

In some case the default setting for uri_protocol does not work properly. To solve this problem just replace
$config['uri_protocol'] = “AUTO” by $config['uri_protocol'] = “REQUEST_URI” from system/application/config/config.php

use of $this->uri->segment(3) in codeigniter pagination


This provides you to retrieve information from your URI strings
$this->uri->segment(n); // n=1 for controller, n=2 for method, etc
it will return
$this->uri->segment(1); // controller
$this->uri->segment(2); // action
$this->uri->segment(3); // 1stsegment
$this->uri->segment(4); // 2ndsegment

pagination in codeigniter

view Code

 <?php
$i=1+$this->uri->segment(3);
if(is_array($displaydata))
{
foreach($displaydata as $db) {
?>

custom fileds...

 <?php $i++;  } } ?>

<?php if(empty($displaydata)) { ?>
            <tr>
            <td colspan="7" bgcolor="#F8F8F8">No Records Found .. .... ...</td>
            </tr>
            <?php }  ?>      
</table>  <?php echo $this->pagination->create_links();?></td>



controller code

public function certificatedata()
{
$displaydata = $this->mreminder->listcertificatedata('','') ;

$total_rows = count($displaydata) ;
$config['base_url'] =  base_url().'index.php/welcome/certificatedata/' ;
$config['per_page'] = 10 ;
$config['full_tag_open'] = '<div>' ;
$config['full_tag_close'] = '</div>' ;
$config['first_link'] = 'First' ;
$config['last_link'] = 'Last' ;
$config['use_page_numbers'] = FALSE ;
$config['prev_link'] = '&lt;';
$config['uri_segment'] = 3 ;
$config['num_links'] = 7 ;
$config['cur_tag_open'] = '<b>' ;
$config['cur_tag_close'] = '</b>' ;
$config['total_rows'] = $total_rows ;

$displaydata = $this->mreminder->listcertificatedata($config['per_page'], $this->uri->segment(3)) ;

$this->pagination->initialize($config);

$data = array(  
'displaydata' => $displaydata    
) ;

$this->load->view('certificatedata',$data);
}


Model COde

function listbillingdata($num, $offset)
{

if($offset!="")
{    
     $que = "select * from billingtbl where deleted=0  LIMIT ".$offset.", ".$num."";
}
else if($num!="")
{
  $que = "select * from billingtbl where deleted=0 LIMIT ".$num."" ;
}
else
   {
$que = "select * from billingtbl where deleted=0" ;
}


$query = $this->db->query($que) ;  
$results = $query->result_array() ;
return $results  ;
}