  | 
              
	              
	                
  
    
	 
        สร้างฟังก์ชั่นก์ เลขบัตรประชาชน เบอร์โทรศัพท์     | 
   
  
    |   | 
   
 
 
 
	
		
			  | 
	   | 
	    | 
		
			  | 
	 
	
		
			  | 
		 		   | 
	  	    
          
            
			
	
			
			 
                สามารถเขียนให้เป็นฟังก์ชั่นเดียวกันได้หรือป่าวครับ 
เบอร์โทรมี 9-10 หลัก เลขประชาชน 13 หลัก 
อยากได้เป็นฟังก์ชั่นก์อ่ะครับ จะได้เอาไปใช้ได้หลายๆจุด 
 
Code (PHP) 
<?php
$tel = 0812345678
$step1 = substr($tel,0,3);
$step2 = substr($tel,3,7);
$stu_tel = $step1."-".$step2;
echo $stu_tel; // 081-2345678
?>
<?php
$id="1579901233420";
$one=substr($id,0,1);
$onemore=$one."-";
$two=substr($id,1,4);
$twomore=$two."-";
$three=substr($id,5,5);
$threemore=$three."-";
$four=substr($id,10,2);
$fourmore=$four."-";
$five=substr($id,12,1);
$fivemore=$five;
$oneid=str_replace($one,$onemore,$one);
$twoid=str_replace($two,$twomore,$two);
$threeid=str_replace($three,$threemore,$three);
$fourid=str_replace($four,$fourmore,$four);
$fiveid=str_replace($five,$fivemore,$five);
$newid=$oneid.$twoid.$threeid.$fourid.$fiveid;;
echo $newid; //1-5799-01233-42-0
?>
 
 
 
  Tag : PHP, JavaScript, jQuery               
                        | 
           
          
            
		
  ประวัติการแก้ไข 2014-07-18 02:06:15	
                             | 
           
          
            
              
                   | 
                   | 
                   | 
               
              
                   | 
                
                    
                      | Date :
                          2014-07-18 02:05:09 | 
                      By :
                          sabaitip | 
                      View :
                          1804 | 
                      Reply :
                          5 | 
                     
                  | 
                   | 
               
              
                   | 
                   | 
                   | 
               
              | 
           
          
            | 
			 | 
           
         
	    
		             | 
		
			  | 
	 
	
		
			  | 
		  | 
		
			  | 
		
			  | 
	 
 
              
  
          
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 Code (ลอง Apply ดูครับ) 
function genNumber($id)
{
	$one=substr($id,0,1);
	$onemore=$one."-";
	$two=substr($id,1,4);
	$twomore=$two."-";
	$three=substr($id,5,5);
	$threemore=$three."-";
	$four=substr($id,10,2);
	$fourmore=$four."-";
	$five=substr($id,12,1);
	$fivemore=$five;
	$oneid=str_replace($one,$onemore,$one);
	$twoid=str_replace($two,$twomore,$two);
	$threeid=str_replace($three,$threemore,$three);
	$fourid=str_replace($four,$fourmore,$four);
	$fiveid=str_replace($five,$fivemore,$five);
	$newid=$oneid.$twoid.$threeid.$fourid.$fiveid;;
	return $newid; //1-5799-01233-42-0
}
                        
               
               | 
             
            
              
			  			  
			                              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2014-07-18 08:51:04 | 
                        By :
                            mr.win | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
               
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 ถ้าเขียนให้เป็นฟังก์ชันเดียวกันไม่รู้ว่าจะทำได้หรือป่าวครับ                        
               
               | 
             
            
              
			                              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2014-07-19 02:06:00 | 
                        By :
                            sabaitip | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
               
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 ได้ครับ 
<?php
function hyphenize($id)
{
	return strlen($id = preg_replace('/[^\d]+/', '', $id)) === 13
		? preg_replace('/^(\d)(\d{4})(\d{5})(\d{2})(\d)$/', '\1-\2-\3-\4-\5', $id)
		: preg_replace('/^((?(?=02)\d{2}|\d{3}))(\d+)$/', '\1-\2', $id);
}
echo hyphenize('1579901233420'); // 1-5799-01233-42-0
echo hyphenize('0812345678'); // 081-2345678
echo hyphenize('029405555'); // 02-9405555
                        
               
               | 
             
            
              
			                              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2014-07-19 02:43:22 | 
                        By :
                            phpinfo() | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
               
		
     
		
	     
	    
     
               
		
     
		
	     
	    
     
      		  
	
     | 
   
 
                 |