<?xml version="1.0" encoding="utf-8"?><rss version="2.0"><channel><title>PHP Learning</title><link>http://www.ninenik.com</link><description>รวมบทความ PHP</description><language>th-TH</language><lastBuildDate>Fri, 10 Feb 2012 02:13:26 +0700</lastBuildDate><copyright>Copyright ninenik.com</copyright><image><title>CMSPro เว็บไซต์สำเร็จรูป</title><url>http://www.ninenik.com/images/logo_ninenik.gif</url><link>http://www.ninenik.com</link></image><item><title>แนวทางการสร้างเมนูหลายภาษา อย่างง่ายด้วย php และ javascript</title><link>http://www.ninenik.com/แนวทางการสร้างเมนูหลายภาษา_อย่างง่ายด้วย_php_และ_javascript-425.html</link><description><![CDATA[<p>&nbsp;ตัวอย่างโค้ดต่อไปนี้ เป็นแนวทางอย่างง่าย สำหรับนำไปประยุกต์ใช้งานการสร้างเว็บไซต์ เมนูหลายภาษา</p>
<div>โดยอาศัยการใช้การกำหนดตัวแปรแบบ Constants ด้วยคำสั่ง define() ใน php&nbsp;</div>
<div>สำหรับการปรับแต่งหรือความต้องการที่เหนือกว่านี้ จำเป็นต้องอาศัยพื้นฐาน หรือทักษะอื่นๆ เพิ่มเติม</div>
<div>ประกอบในการนำไปประยุกต์ใช้งาน</div>
<div>&nbsp;</div>
<div><strong>ดูตัวอย่างได้ที่</strong></div>
<div><a href="http://www.ninenik.com/demo/multilang/">http://www.ninenik.com/demo/multilang/</a></div>
<div>&nbsp;</div>
<div><strong>ตัวอย่างไฟล์ จะมีแค่ 3 ไฟล์ 2 โฟลเดอร์</strong></div>
<div>1.index.php ไฟล์หลักสำหรับทดสอบการแสดง</div>
<div>2.en/index.php ไฟล์สำหรับเมนูภาษาอังกฤษ</div>
<div>3.th/index.php &nbsp;ไฟล์สำหรับเมนูภาษาไทย</div>
<div>&nbsp;</div>
<div>&nbsp;</div>
<div><strong>1.index.php ไฟล์หลักสำหรับทดสอบการแสดง</strong></div>
<pre name="code" class="php:controls">
&lt;?php
// ส่วนกำหนดสำหรับการเรียกใช้เมนูภาษา ตามที่ผู้ใช้เลือก กรณีเริ่มต้น จะเป็นภาษาอังกฤษ
$pathLang=(!isset($_COOKIE['ck_lang']) || $_COOKIE['ck_lang']==&quot;&quot;)?&quot;en&quot;:$_COOKIE['ck_lang'];
include($pathLang.&quot;/index.php&quot;);
?&gt;
&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; 
&quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;
&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;
&lt;head&gt;
&lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=utf-8&quot; /&gt;
&lt;title&gt;&lt;/title&gt;
&lt;/head&gt;

&lt;body&gt;


&lt;div style=&quot;margin:auto;width:600px;&quot;&gt;
&lt;br /&gt;
&lt;a href=&quot;javascript:setLang('th');&quot;&gt;Thai&lt;/a&gt;
&lt;a href=&quot;javascript:setLang('en');&quot;&gt;Eng&lt;/a&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;a href=&quot;#&quot;&gt;&lt;?=_Home?&gt;&lt;/a&gt; |  
&lt;a href=&quot;#&quot;&gt;&lt;?=_About_Us?&gt;&lt;/a&gt; |  
&lt;a href=&quot;#&quot;&gt;&lt;?=_Contact_Us?&gt;&lt;/a&gt; 

 &lt;/div&gt;




&lt;script type=&quot;text/javascript&quot;&gt;
function setLang(langID){ //ฟังก์ชัน javascript สำหรับกำหนด ตัวแปร cookie ภาษา
	var days=365; // กำหนดจำนวนวันที่ต้องการให้จำค่า  
	var date = new Date();  
	date.setTime(date.getTime()+(days*24*60*60*1000));  
	var expires = &quot;; expires=&quot;+date.toGMTString();  
	document.cookie = &quot;ck_lang=&quot; +langID+ &quot;; expires=&quot; + expires + &quot;; path=/&quot;;  
	window.location.reload()
}
&lt;/script&gt;
 
&lt;/body&gt;
&lt;/html&gt;</pre>
<div>&nbsp;</div>
<div><strong>2.en/index.php ไฟล์สำหรับเมนูภาษาอังกฤษ</strong></div>
<pre name="code" class="php:controls">
&lt;?php
define(&quot;_Home&quot;,&quot;Home&quot;);
define(&quot;_About_Us&quot;,&quot;About Us&quot;);
define(&quot;_Contact_Us&quot;,&quot;Contact Us&quot;);
?&gt;</pre>
<div>&nbsp;</div>
<div><strong>3.th/index.php &nbsp;ไฟล์สำหรับเมนูภาษาไทย</strong></div>
<pre name="code" class="php:controls">
&lt;?php
define(&quot;_Home&quot;,&quot;หน้าแรก&quot;);
define(&quot;_About_Us&quot;,&quot;เกี่ยวกับเรา&quot;);
define(&quot;_Contact_Us&quot;,&quot;ติดต่อเรา&quot;);
?&gt;</pre>
<div>&nbsp;</div>
<div>&nbsp;</div>
<div>&nbsp;</div>
<div><strong>ดาวโหลดไฟล์ตัวอย่าง อย่างง่าย ได้ที่</strong></div>
<div><a href="http://www.ninenik.com/download/multilang.rar">http://www.ninenik.com/download/multilang.rar</a></div>]]></description><pubDate>Wed, 09 Nov 2011 11:59:58 +0700</pubDate></item><item><title>การเชื่อมต่อ facebook และ ล็อกอิน แบบ popup ด้วย php sdk v.3.1.1</title><link>http://www.ninenik.com/การเชื่อมต่อ_facebook_และ_ล็อกอิน_แบบ_popup_ด้วย_php_sdk_v.3.1.1-424.html</link><description><![CDATA[<p>&nbsp;แนวทางการประยุกต์ การเชื่อมต่อ facebook ด้วย php sdk v.3.1.1 แบบสร้าง ล็อกอิน</p>
<div>แบบ popup window ด้วย javascript</div>
<div>&nbsp;</div>
<div>โดยจะเป็นการประยุกต์ใช้งานจากเนื้อหาเดิม ตามลิ้งค์ด้านล่าง</div>
<div>&nbsp;</div>
<div><a target="_blank" href="http://www.ninenik.com/content.php?arti_id=423">http://www.ninenik.com/content.php?arti_id=423</a></div>
<div>&nbsp;</div>
<div>รูปแบบการทำงานคือ เมื่อทำการคลิกที่ลิ้งค์ สำหรับล็อกอินด้วย facebook ก็จะเปิดหน้า popup</div>
<div>สำหรับกรอกชื่อผู้ใช้ หรือรหัสผ่าน กรณียังไม่ได้ล็อกอิน facebook อยู่ หลังจาก ล็อกอินเสร็จแล้ว</div>
<div>จะลิ้งค์ไปอีกหน้าหนึ่ง สำหรับสร้างค่า session และเช็ค สถานะการล็อกอิน ของผู้ใช้งาน&nbsp;</div>
<div>แล้วปิดหน้า popup นั้นลง พร้อมกับรีโหลดหน้าหลัก เป็นต้น</div>
<div>&nbsp;</div>
<div>ดาวน์โหลดไฟล์ทดสอบ ไปปรับใช้ได้ที่</div>
<div><a target="_blank" href="http://www.ninenik.com/fb/fb3.1.1popup.rar">http://www.ninenik.com/fb/fb3.1.1popup.rar</a></div>
<div>&nbsp;</div>
<div>หรือทดสอบตัวอย่างก่อนโหลด</div>
<div><a target="_blank" href="http://www.ninenik.com/fb/fb3.1.1/test_login_popup.php">http://www.ninenik.com/fb/fb3.1.1/test_login_popup.php</a></div>
<div>&nbsp;</div>
<div>ไฟล์โค้ดตัวอย่าง ที่เกี่ยวข้อง</div>
<div>&nbsp;</div>
<div><strong>ไฟล์&nbsp;test_login_popup.php</strong></div>
<pre name="code" class="php:controls">
&lt;?php
include(&quot;fb_connect_popup.php&quot;);
?&gt;
&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; 
&quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;
&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot; xmlns:fb=&quot;http://www.facebook.com/2008/fbml&quot;&gt;
&lt;head&gt;
&lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=utf-8&quot; /&gt;
&lt;title&gt;Sample FB Login Popup&lt;/title&gt;
&lt;/head&gt;

&lt;body&gt;
&lt;?php
//pre($fb_user);
?&gt;
&lt;h1&gt;php-sdk 3.1.1&lt;/h1&gt;

&lt;?php if($fb_user){ // ถ้ามีการล็อกอิน facebook อยู่แล้ว แสดงลิ้งค์สำหรับ logout ?&gt;
	&lt;a href=&quot;&lt;?=$logoutUrl?&gt;&quot;&gt;Logout&lt;/a&gt;
&lt;?php }else{ //  ถ้ายังไม่ได้ล็อกอิน แสดงลิ้งค์สำหรับ Login ?&gt;
    &lt;div&gt;
    Login using OAuth 2.0 handled by the PHP SDK:
    &lt;a href=&quot;javascript:popup('&lt;?=$loginUrl?&gt;','',500,350);&quot;&gt;Login with Facebook&lt;/a&gt;
    &lt;/div&gt;
&lt;?php } ?&gt;


    &lt;h3&gt;PHP Session&lt;/h3&gt;
    &lt;pre&gt;&lt;?php print_r($_SESSION); ?&gt;&lt;/pre&gt;

&lt;?php if($fb_user){ // ถ้ามีการล็อกอิน facebook อยู่แล้ว แสดงข้อมูลของคนๆ นั้น ?&gt;
      &lt;h3&gt;You&lt;/h3&gt;
      &lt;img src=&quot;https://graph.facebook.com/&lt;?=$fb_user?&gt;/picture&quot;&gt;

      &lt;h3&gt;Your User Object (/me)&lt;/h3&gt;
      &lt;?php 
	  pre($fb_userData); 
	  ?&gt;
      
&lt;?php }else{ //  ถ้ายังไม่ได้ล็อกอิน  ?&gt;
      &lt;strong&gt;&lt;em&gt;You are not Connected.&lt;/em&gt;&lt;/strong&gt;

&lt;?php } ?&gt;


&lt;script type=&quot;text/javascript&quot;&gt;
function popup(url,name,windowWidth,windowHeight){    
	myleft=(screen.width)?(screen.width-windowWidth)/2:100;	
	mytop=(screen.height)?(screen.height-windowHeight)/2:100;	
	properties = &quot;width=&quot;+windowWidth+&quot;,height=&quot;+windowHeight;
	properties +=&quot;,scrollbars=yes, top=&quot;+mytop+&quot;,left=&quot;+myleft;   
	window.open(url,name,properties);
}
&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;</pre>
<div>&nbsp;</div>
<div>&nbsp;</div>
<div>
<div><strong>ไฟล์&nbsp;fb_connect_popup.php</strong></div>
<pre name="code" class="php:controls">
&lt;?php
session_start(); // กำหนดไว้ กรณีอาจได้ใช้ตัวแปร session
include(&quot;inc/facebook.php&quot;); //  เรียกใช้งานไฟล์ php-sdk สำหรับ facebook
//
$facebook = new Facebook(array(
  'appId'  =&gt; 'xxxxxx', // appid ที่ได้จาก facebook
  'secret' =&gt; 'xxxxxxxx',  // app secret ที่ได้จาก facebook
  'fileUpload' =&gt; true, // เปิดใช้ในส่วนของการอัพโหลดรูปได้
  'cookie' =&gt; true, // อนุญาตใช้งาน cookie  
));
// สร้างฟังก์ชันไว้สำหรัดทดสอบ การแสดงผลการใช้งาน
function pre($varUse){
	echo &quot;&lt;pre&gt;&quot;;
	print_r($varUse);
	echo &quot;&lt;/pre&gt;&quot;;
}
// Get User ID
$fb_user = $facebook-&gt;getUser();
if($fb_user){
  try{
    // Proceed knowing you have a logged in user who's authenticated.
    $fb_userData=$facebook-&gt;api('/me');
  }catch(FacebookApiException $e) {
    error_log($e);
    $user=null;
  }
}
if(isset($_GET['logout'])){ // ทำการ logout อย่างสมบูรณ์
	$facebook-&gt;destroySession(null); 	// ล่างค่า session ของ facebook
	header(&quot;Location:&quot;.$_SERVER['PHP_SELF']); //ลิ้งค์ไปหน้าที่ต้องการเมื่อ logout เรียบร้อยแล้ว
}
// Login or logout url will be needed depending on current user state.
if($fb_user){
  $logoutUrl = $facebook-&gt;getLogoutUrl(array(
  	&quot;next&quot;=&gt;&quot;http://www.ninenik.com/fb/fb3.1.1/test_login_popup.php?logout&quot;
  ));
} else{
  $loginUrl = $facebook-&gt;getLoginUrl(array(
  	&quot;redirect_uri&quot;=&gt;&quot;http://www.ninenik.com/fb/fb3.1.1/fb_checklogin.php&quot;,
	&quot;display&quot;=&gt;&quot;popup&quot;,
	&quot;scope&quot;=&gt;&quot;offline_access,publish_stream,email&quot; // คั่นแต่ละค่าด้วย ,(comma
  ));
}
?&gt;</pre>
<div>&nbsp;</div>
<div>&nbsp;</div>
<div>
<div><strong>ไฟล์&nbsp;fb_checklogin.php</strong></div>
<pre name="code" class="php:controls">
&lt;?php
include(&quot;fb_connect_popup.php&quot;);
?&gt;
&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;
&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;
&lt;head&gt;
&lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=utf-8&quot; /&gt;
&lt;title&gt;Check Login&lt;/title&gt;
&lt;/head&gt;

&lt;body&gt;
&lt;script type=&quot;text/javascript&quot;&gt;
window.opener.location.reload();
window.close();
&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;</pre>
<div>&nbsp;</div>
</div>
</div>
<div>&nbsp;</div>]]></description><pubDate>Fri, 14 Oct 2011 11:49:11 +0700</pubDate></item><item><title>แนะนำการใช้งาน การเชื่อมต่อ facebook ด้วย php sdk v.3.1.1</title><link>http://www.ninenik.com/แนะนำการใช้งาน_การเชื่อมต่อ_facebook_ด้วย_php_sdk_v.3.1.1-423.html</link><description><![CDATA[<p>&nbsp;ตัวอย่างโค้ดต่อไปนี้ เป็นแนวทางการเชื่อมต่อกับ facebook ด้วย php sdk&nbsp;</p>
<div>เวอร์ชั่น 3.1.1จะมีไฟล์สำหรับให้โหลด ไปทดสอบตามลิ้งค์ ด้านล่าง</div>
<div>&nbsp;</div>
<div><a target="_blank" href="http://www.ninenik.com/fb/fb3.1.1.rar">http://www.ninenik.com/fb/fb3.1.1.rar</a></div>
<div>&nbsp;</div>
<div>หรือ ดูตัวอย่างก่อนโหลดได้ที่&nbsp;</div>
<div><a target="_blank" href="http://www.ninenik.com/fb/fb3.1.1/test_login.php">http://www.ninenik.com/fb/fb3.1.1/test_login.php</a></div>
<div>&nbsp;</div>
<div>ในเว็บจะมีบทความ ที่เกี่ยวกับ การเชื่อมต่อกับ facebook หลายเนื้อหา แต่บางรายการ</div>
<div>ก็เป็นไปแบบไม่อัพเดท หรือ มีการพัฒนาเพิ่มเติมมาใหม่แล้ว ตัวเก่าอาจจะผิดเพี้ยน หรือใช้งานไม่ได้</div>
<div>หากเพิ่งเริ่มศึกษา แนะนำให้ค้นหาบทความ แล้วดูวันที่ของเนื้อหา และตัวอย่างที่นำมาประกอบ</div>
<div>ก่อนพิจารณาไปนำใช้ใช้งาน</div>
<div>&nbsp;</div>
<div>ส่วนสำหรับแก้ไขจะอยู่ในไฟล์ fb_connect.php</div>
<div>&nbsp;</div>
<pre name="code" class="php:controls">
&lt;?php
session_start(); // กำหนดไว้ กรณีอาจได้ใช้ตัวแปร session
include(&quot;inc/facebook.php&quot;); //  เรียกใช้งานไฟล์ php-sdk สำหรับ facebook
//
$facebook = new Facebook(array(
  'appId'  =&gt; 'xxxxxx', // appid ที่ได้จาก facebook
  'secret' =&gt; 'xxxxx',  // app secret ที่ได้จาก facebook
  'fileUpload' =&gt; true, // เปิดใช้ในส่วนของการอัพโหลดรูปได้
  'cookie' =&gt; true, // อนุญาตใช้งาน cookie  
));
// สร้างฟังก์ชันไว้สำหรัดทดสอบ การแสดงผลการใช้งาน
function pre($varUse){
	echo &quot;&lt;pre&gt;&quot;;
	print_r($varUse);
	echo &quot;&lt;/pre&gt;&quot;;
}
// Get User ID
$fb_user = $facebook-&gt;getUser();
if($fb_user){
  try{
    // Proceed knowing you have a logged in user who's authenticated.
    $fb_userData=$facebook-&gt;api('/me');
  }catch(FacebookApiException $e) {
    error_log($e);
    $user=null;
  }
}
if(isset($_GET['logout'])){ // ทำการ logout อย่างสมบูรณ์
	$facebook-&gt;destroySession(null); 	// ล่างค่า session ของ facebook
	header(&quot;Location:&quot;.$_SERVER['PHP_SELF']); //ลิ้งค์ไปหน้าที่ต้องการเมื่อ logout เรียบร้อยแล้ว
}
// Login or logout url will be needed depending on current user state.
if($fb_user){
  $logoutUrl = $facebook-&gt;getLogoutUrl(array(
  	&quot;next&quot;=&gt;&quot;http://www.ninenik.com/fb/fb3.1.1/test_login.php?logout&quot;
  ));
} else{
  $loginUrl = $facebook-&gt;getLoginUrl(array(
  	&quot;redirect_uri&quot;=&gt;&quot;http://www.ninenik.com/fb/fb3.1.1/test_login.php&quot;,
	&quot;display&quot;=&gt;&quot;popup&quot;,
	&quot;scope&quot;=&gt;&quot;offline_access,publish_stream,email&quot; // คั่นแต่ละค่าด้วย ,(comma
  ));
}
?&gt;</pre>
<div>&nbsp;</div>]]></description><pubDate>Thu, 13 Oct 2011 16:57:42 +0700</pubDate></item><item><title>การสร้าง active เมนู อย่างง่าย ด้วย css และ php</title><link>http://www.ninenik.com/การสร้าง_active_เมนู_อย่างง่าย_ด้วย_css_และ_php-414.html</link><description><![CDATA[<p>&nbsp;การสร้าง active เมนู อย่างง่าย ด้วย css และ php</p>
<div>&nbsp;</div>
<div>ตัวอย่างโค้ดต่อไปนี้ เป้นหนึ่งในหลายวิธี ในการกำหนด สถานะการ active ของลิ้งค์ปัจจุบัน</div>
<div>โดยอาศัย php ในการเช็ค url ของหน้าปัจจุบัน ไปกำหนด css ของเมนู</div>
<div>&nbsp;</div>
<div>ตัวอย่างประกอบ</div>
<div><a target="_blank" href="http://www.ninenik.com/demo/css_menu_active.php">http://www.ninenik.com/demo/css_menu_active.php</a></div>
<div>&nbsp;</div>
<div>โค้ดตัวอย่างทั้งหมด</div>
<div>&nbsp;</div>
<pre name="code" class="php:controls">
&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; 
&quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;
&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;
&lt;head&gt;
&lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=utf-8&quot; /&gt;
&lt;title&gt;css_menu_active&lt;/title&gt;

&lt;style type=&quot;text/css&quot;&gt;
/* เมนูปกติ */
.css_menu{
	position:relative;
	display:block;
	float:left;
	width:100px;
	height:25px;
	background-color:#333;
	color:#FFF;	
	text-align:center;
	text-decoration:none;
	line-height:25px;
	margin-right:2px;
}
/*	เมนูเมื่อเมาส์ over*/
.css_menu:hover{
	position:relative;
	display:block;
	float:left;
	width:100px;
	height:25px;
	background-color:#CCC;
	color:#000;	
}
/*	เมนูเมื่อ active*/
.css_menu_active{
	position:relative;
	display:block;
	float:left;
	width:100px;
	height:25px;
	background-color:#CCC;
	color:#000;	
	text-align:center;
	text-decoration:none;
	line-height:25px;
	margin-right:2px;
}
&lt;/style&gt;
&lt;/head&gt;

&lt;body&gt;

&lt;?php
// ฟังก์ชั่นอย่างง่ายใช้ทดสอบ หน้าปัจจุบัน
function active_menu($link_chk,$default_active=0){
	if($default_active==1){
		return ($_SERVER['REQUEST_URI']==$link_chk || $_SERVER['QUERY_STRING']==&quot;&quot;)?&quot;_active&quot;:&quot;&quot;;
	}else{
		return ($_SERVER['REQUEST_URI']==$link_chk)?&quot;_active&quot;:&quot;&quot;;
	}
}
?&gt;
&lt;a class=&quot;css_menu&lt;?=active_menu(&quot;/demo/css_menu_active.php?menu=1&quot;,1)?&gt;&quot; href=&quot;css_menu_active.php?menu=1&quot;&gt;Menu 1&lt;/a&gt;&amp;nbsp;
&lt;a class=&quot;css_menu&lt;?=active_menu(&quot;/demo/css_menu_active.php?menu=2&quot;)?&gt;&quot; href=&quot;css_menu_active.php?menu=2&quot;&gt;Menu 2&lt;/a&gt;&amp;nbsp;
&lt;a class=&quot;css_menu&lt;?=active_menu(&quot;/demo/css_menu_active.php?menu=3&quot;)?&gt;&quot; href=&quot;css_menu_active.php?menu=3&quot;&gt;Menu 3&lt;/a&gt;&amp;nbsp;
&lt;a class=&quot;css_menu&lt;?=active_menu(&quot;/demo/css_menu_active.php?menu=4&quot;)?&gt;&quot; href=&quot;css_menu_active.php?menu=4&quot;&gt;Menu 4&lt;/a&gt;&amp;nbsp;
&lt;/body&gt;
&lt;/html&gt;</pre>
<div>&nbsp;</div>
<div>&nbsp;</div>
<div>การใช้งาน&nbsp;</div>
<div>หลักการอย่างง่าย คือการส่งค่าว่า หน้าปัจจุบัน url คืออะไร เช่น</div>
<div>&nbsp;</div>
<pre name="code" class="php:controls">
&lt;?=active_menu(&quot;/demo/css_menu_active.php?menu=2&quot;)?&gt;</pre>
<div>&nbsp;</div>
<div>&nbsp;</div>
<div>หมายถึง ถ้า ตัวแปร $_SERVER['REQUEST_URI'] เท่ากับ /demo/css_menu_active.php?menu=2</div>
<div>ก็ให้เติมคำว่า _active ต่อเข้าไปใน css class&nbsp;</div>
<div>&nbsp;</div>
<div>อักตัวอย่าง</div>
<pre name="code" class="php:controls">
&lt;?=active_menu(&quot;/demo/css_menu_active.php?menu=1&quot;,1)?&gt;</pre>
<div>&nbsp;</div>
<div>&nbsp;</div>
<div>หมายถึง ถ้า ตัวแปร $_SERVER['REQUEST_URI'] เท่ากับ /demo/css_menu_active.php?menu=1</div>
<div>หรือ ไม่มีเงื่อนไขใดๆ ถูกต้อง ก็ให้เติมคำว่า _active ต่อเข้าไปใน css class ของเมนูนั้น เพื่อเป็นค่าเริ่มต้น</div>]]></description><pubDate>Thu, 23 Jun 2011 09:52:47 +0700</pubDate></item><item><title>แนวทางการกำหนด expired header ให้กับรูปภาพ ด้วย php อย่างง่าย</title><link>http://www.ninenik.com/แนวทางการกำหนด_expired_header_ให้กับรูปภาพ_ด้วย_php_อย่างง่าย-413.html</link><description><![CDATA[<p>&nbsp;ตัวอย่างต่อไปนี้ เป็นแนวทางสำหรับการกำหนด expired header ให้กับรูปภาพ ที่แสดงในหน้าเว็บไซต์</p>
<div>ทำให้ บราวเซอร์เลือกใช้งานรูปภาพจากเครื่อง โดยดูจากวันที่ expired ของรูปภาพที่เรากำหนด ถ้ารูปที่อยู่</div>
<div>ในเครื่องหรือที่ cache ไว้ในตอนแรก ยังไม่หมดอายุ ก็จะทำการดึงรูปจากที่เครื่องมาใช้ แทนการไปโหลด</div>
<div>ที่ server ใหม่ทุกครั้ง&nbsp;</div>
<div>&nbsp;</div>
<div>1. สรัางไฟล์ index.php ตามโค้ดด้านล่าง แล้วในไปวางไว้ในโฟลเดอร์รูปภาพ</div>
<pre name="code" class="php:controls">
&lt;?php
if(!isset($_GET['pic']) || $_GET['pic']==&quot;&quot;){
	$_GET['pic']=&quot;photoNotAvailable.jpg&quot;; // ชื่อรูปภาพที่ต้องการแสดงกรณีไม่มีรูป
}
function imgType($imgName){
	$imgName=strtolower($imgName);
	$imgTypeShow=&quot;&quot;;
	if(preg_match(&quot;/.gif/&quot;,$imgName)){
		$imgTypeShow=&quot;gif&quot;;
	}
	if(preg_match(&quot;/.png/&quot;,$imgName)){
		$imgTypeShow=&quot;png&quot;;
	}
	if(preg_match(&quot;/.jpg/&quot;,$imgName)){
		$imgTypeShow=&quot;jpeg&quot;;
	}
	if(preg_match(&quot;/.jpeg/&quot;,$imgName)){
		$imgTypeShow=&quot;jpeg&quot;;
	}		
	return $imgTypeShow;	
}
if(imgType($_GET['pic'])){
$last_modified_time = filemtime($_GET['pic']); 
$etag = md5_file($_GET['pic']); 
header(&quot;Content-type: image/&quot;.imgType($_GET['pic']).&quot;;&quot;);  
header(&quot;Cache-Control: must-revalidate&quot;);
header(&quot;Last-Modified: &quot;.gmdate(&quot;D, d M Y H:i:s&quot;, $last_modified_time).&quot; GMT&quot;); 
header(&quot;Etag: $etag&quot;); 
$offset = 60 * 60 * 24 * 30; // กำหนดอายุ 30 วัน ดูตัวคูณตัวหลังสุด * 30
$ExpStr = &quot;Expires: &quot; . gmdate(&quot;D, d M Y H:i:s&quot;, time() + $offset) . &quot; GMT&quot;;
header($ExpStr);
if($_GET['pic']!=&quot;&quot;){
	if(is_file($_GET['pic'])){
		$abs_url=&quot;htttp://www.yourdomain.com/images/&quot;;// กำหนด path หรือตำแหน่งจริงของรูปภาพที่จะแสดง
		echo file_get_contents($abs_url.$_GET['pic']);
	}
}
}
?&gt;</pre>
<div>
<div>2. การใช้งาน ตัวอย่างรูปภาพตาม url นี้ http://www.ninenik.com/images/logo_01_Mon.gif</div>
<div>&nbsp;</div>
<div>นำไฟล์ index.php ไปไว้ที่โฟลเดอร์ images</div>
<div>เรียกใช้งาน รูปภาพผ่าน url ใหม่เป็น&nbsp;</div>
<div>&nbsp;</div>
<div>http://www.ninenik.com/images/?pic=logo_01_Mon.gif</div>
<div>&nbsp;</div>
<div>เท่านี้ก็จะได้รูปภาพที่มีการกำหนด expired header มาแสดง</div>
</div>]]></description><pubDate>Mon, 06 Jun 2011 09:37:41 +0700</pubDate></item><item><title>สร้างรูปแบบ วันที่ คล้าย วันที่ใน facebook comment ด้วย php อย่างง่าย</title><link>http://www.ninenik.com/สร้างรูปแบบ_วันที่_คล้าย_วันที่ใน_facebook_comment_ด้วย_php_อย่างง่าย-410.html</link><description><![CDATA[<p>ตัวอย่างรูปแบบฟังก์ชัน php ต่อไปนี้เป็นรูปแบบ การกำหนด การแสดงข้อมูลวันที่ ในลักษณะคล้าย<br />
กับวันที่ที่แสดงใน facebook comment สามารถนำไปประยุกต์ หรือปรับแต่งเพิ่มเติมได้ตามต้องการ</p>
<p><strong>รูปแบบการแสดงจะเป้นไป ดังนี้</strong><br />
เวลาในวินาที <br />
ผ่านมา 1 วินาที <br />
<strong>1 second ago</strong><br />
ผ่านมา 3 หรือ 4 วินาที <br />
<strong>a few seconds ago</strong><br />
ผ่านมา 1 นาที<br />
<strong>1 minute ago</strong><br />
ผ่านมา 3 หรือ 4 นาที<br />
<strong>a few minutes ago</strong><br />
ผ่านมาแล้ว 1 ชั่วโมง<br />
<strong>about an hour ago<br />
</strong>ผ่านมาแล้ว 7 ชั่วโมง<br />
<strong>7 hours ago</strong><br />
ผ่านมาแล้ว 1 วัน (เมื่อวาน)<br />
<strong>Yesterday at 11:28am </strong><br />
วันอื่นๆ ภายในปีปัจจุบัน<br />
<strong>December 28 at 9:08am</strong><br />
วันอื่นๆ ปีก่อนหน้า<br />
<strong>December 28, 2010 at 9:08am</strong></p>
<p>โค้ดฟังก์ชัน</p>
<pre class="php:controls" name="code">
&lt;?php
function fb_date($timestamp){
	$difference = time() - $timestamp;
	$periods = array(&quot;second&quot;, &quot;minute&quot;, &quot;hour&quot;);
	$ending=&quot; ago&quot;;
	if($difference&lt;60){
		$j=0;
		$periods[$j].=($difference != 1)?&quot;s&quot;:&quot;&quot;;
		$difference=($difference==3 || $difference==4)?&quot;a few &quot;:$difference;
		$text = &quot;$difference $periods[$j] $ending&quot;;
	}elseif($difference&lt;3600){
		$j=1;
		$difference=round($difference/60);
		$periods[$j].=($difference != 1)?&quot;s&quot;:&quot;&quot;;
		$difference=($difference==3 || $difference==4)?&quot;a few &quot;:$difference;
		$text = &quot;$difference $periods[$j] $ending&quot;;		
	}elseif($difference&lt;86400){
		$j=2;
		$difference=round($difference/3600);
		$periods[$j].=($difference != 1)?&quot;s&quot;:&quot;&quot;;
		$difference=($difference != 1)?$difference:&quot;about an &quot;;
		$text = &quot;$difference $periods[$j] $ending&quot;;		
	}elseif($difference&lt;172800){
		$difference=round($difference/86400);
		$periods[$j].=($difference != 1)?&quot;s&quot;:&quot;&quot;;
		$text = &quot;Yesterday at &quot;.date(&quot;g:ia&quot;,$timestamp);								
	}else{
		if($timestamp&lt;strtotime(date(&quot;Y-01-01 00:00:00&quot;))){
			$text = date(&quot;l j, Y&quot;,$timestamp).&quot; at &quot;.date(&quot;g:ia&quot;,$timestamp);		
		}else{
			$text = date(&quot;l j&quot;,$timestamp).&quot; at &quot;.date(&quot;g:ia&quot;,$timestamp);			
		}
	}
	return $text;
}
?&gt;</pre>
<p><font face="Courier New">ตัวอย่างการใช้งาน</font></p>
<pre class="php:controls" name="code">
ถ้าเก็บเวลาในรูปแบบ timestamp (ตัวอย่าง 1300950558)
$date_you=1300950558;
echo fb_date($date_you);</pre>
<pre class="php:controls" name="code">
ถ้าเก็บเวลาในรูปแบบ  datetime (ตัวอย่าง 2011-03-24 15:30:50)
$date_you=&quot;2011-03-24 15:30:50&quot;;
echo fb_date(strtotime($date_you));</pre>
<p>&nbsp;</p>]]></description><pubDate>Fri, 25 Mar 2011 10:06:41 +0700</pubDate></item><item><title>ทบทวน htaccess สำหรับ ให้ url เว็บมี www ด้านหน้าเสมอ</title><link>http://www.ninenik.com/ทบทวน_htaccess_สำหรับ_ให้_url_เว็บมี_www_ด้านหน้าเสมอ-404.html</link><description><![CDATA[<p>ตัวอย่างคำสั่ง htaccess สำหรับกันลืม หรือไว้หยิบใช้งาน เมื่อจำเป็น โดยใช้สำหรับ<br />
กำหนดให้เว็บไซต์ของเรา เติม www ด้านหน้าให้กับ url เสมอในกรณีที่ไม่ได้พิมพ์<br />
ตัวอย่างเช่น ถ้าเราต้องการเข้าเว็บไซต์ <a href="http://www.ninenik.com">www.ninenik.com</a><br />
แต่เราพิมพ์แค่ ninenik.com โดยไม่พิมพ์ www ก็ยังสามารถเข้าเว็บไซต์ได้<br />
หากเราต้องการให้ เมื่อ พิมพ์แค่ ninenik.com ให้ url เปลี่ยนเป็น <a href="http://www.ninenik.com">www.ninenik.com</a><br />
หรือมี www ด้านหน้าให้โดยอัตโนมัติ สามารถใช้ ไฟล์ .htaccess ที่มีคำสั่งดังนี้<br />
&nbsp;</p>
<pre class="php:controls" name="code">
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]</pre>
<p>&nbsp;</p>]]></description><pubDate>Fri, 18 Mar 2011 08:00:00 +0700</pubDate></item><item><title>การใช้งาน character classes ใน Regular Expressions ของ PHP</title><link>http://www.ninenik.com/การใช้งาน_character_classes_ใน_Regular_Expressions_ของ_PHP-396.html</link><description><![CDATA[<p>รูปแบบการขึ้นต้นด้วย เครื่องหมายปีกกาสี่เหลี่ยมเปิด ( [ ) และปิดด้วยด้วยเครื่องหมายปีกกาสี่เหลี่ยมปิด ( ] ) เป้นการใช้งาน Regular Expressions ในส่วนของ character class</p>
<p>โดยหลักของ Character class ตัวอักขระที่อยู่ในปีกกาสี่เหลี่ยม เปิด ปิด ( [] ) หมายถึงเงื่อนไขว่า<br />
มีตัวอักขระ นั้นๆ&nbsp; ตัวหนึ่งตัวใด หรือไม่ เช่น<br />
<strong>ตัวอย่าง<br />
รูปแบบ pattern /[aeiou]/<br />
</strong>- หมายถึง ถ้ามีตัวอักษร a หรือ e หรือ i หรือ o หรือ u จะเข้าเงื่อนไขของรูปแบบ pattern</p>
<p><strong>ตัวอย่างการใช้งานร่วมกับ โค้ด PHP <br />
</strong></p>
<pre class="php:controls" name="code">
&lt;?php
$data_test=&quot;hotmail google yahoo geegle facebook&quot;;
echo preg_match_all('/[hfg]/',$data_test,$matches);
// ค่า echo ที่ได้จะเท่ากับ 7 ซึ่งคือจำนวนค่าที่ตรงกับเงือนไขตามรูปแบบ pattern
// โดย เป็นตัว h 2 ตัว ตัว g 4 ตัว และตัว f อีก 1 ตัว
// อยู่ในตัวแปรอาเรย์ชื่อว่า $matches
// มีค่าตังนี้คือ $matches[0]=h;
// $matches[0] เท่ากับ h;
// $matches[1] เท่ากับ g;
// $matches[2] เท่ากับ g;
// $matches[3] เท่ากับ h;
// $matches[4] เท่ากับ g;
// $matches[5] เท่ากับ g;
// $matches[6] เท่ากับ f;
?&gt;</pre>
<p><strong>หมายเหตุ::&nbsp; preg_match_all จะต่างจาก preg_match <br />
</strong>ตรงที่ preg_match จะตรวจสอบจนพบค่าแรกที่ตรงกับเงื่อนไข pattern ค่าเดียวเท่านั้น แล้วหยุดการตรวจสอบ<br />
ขณะที่ preg_match_all จะทำการตรวจสอบค่าทั้งหมดที่ตรงกับเงื่อนไข pattern</p>
<p>รูปแบบ pattern จะเข้าเงื่อนไข เมื่อ ตัวอักขระ ตัวหนึ่งตัวใดปรากฏในข้อความที่ต้องการตรวจสอบ เว้นเสียแต่<br />
ที่ตัวอักขระตัวแรกเป็นเครื่องหมาย ( ^ ในที่นี้ ขอเรียกว่าเครื่องหมายรูปหมวก) ซึ่งหมายถึง ต้องไม่มีอักขระใดๆ ที่<br />
แสดงใน character class <br />
<strong>ตัวอย่าง<br />
รูปแบบ pattern /[^aeiou]/<br />
</strong>- หมายถึง ถ้ามีตัวอักษรที่ไม่ใช่ a หรือ e หรือ i หรือ o หรือ u จะเข้าเงื่อนไขของรูปแบบ pattern</p>
<div style="display:block;clear:both;float:left;height:5px;border-bottom:1px dashed #000000;width:100%;">&nbsp;</div><br />
<p>สำหรับในกรณีที่ต้องมีการตรวจสอบเครื่องหมายรูปหมวก (^) สามารถกำหนดได้โดย ต้องไม่ให้เครืองหมาย <br />
รูปหมวก (^) อยู่ที่ตำแหน่งแรก หรือ จะใช้เครื่องหมาย ( \ ) เมื่อต้องการตรวจสอบอักขระนี้ในรูปแบบ pattern เช่น</p>
<p><strong>รูปแบบ pattern /[aeio\^u]/<br />
</strong>- หมายถึง ถ้ามีตัวอักษร a หรือ e หรือ i หรือ o หรือ ^ หรือ u จะเข้าเงื่อนไขของรูปแบบ pattern</p>
<p>ถ้ามีการกำหนด Pattern Modifiers เป็น case-insensitive คือใช้ตัว i ซึ่งจะหมายถึง เป็นตัวพิมพ์เล็ก หรือตัว<br />
พิมพ์ใหญ่ก็ได้ ( อ่านเพิ่มเติมเกี่ยวกับ Pattern Modifiers ได้ที่ <a href="http://www.php.net/manual/en/reference.pcre.pattern.modifiers.php">http://www.php.net/manual/en/reference.pcre.pattern.modifiers.php</a> )</p>
<p><strong>ตัวอย่าง<br />
รูปแบบ pattern /[aeiou]/i<br />
</strong>- หมายถึง ถ้ามีตัวอักษร a หรือ e หรือ i หรือ o หรือ u รวมถึงตัวพิมพ์ใหญ่ด้วย จะเข้าเงื่อนไขของรูปแบบ pattern</p>
<p><strong>ตัวอย่าง<br />
รูปแบบ pattern /[^aeiou]/i<br />
</strong>- หมายถึง ถ้ามีตัวอักษรที่ไม่ใช่ a หรือ e หรือ i หรือ o หรือ u รวมถึงตัวพิมพ์ใหญ่ด้วย จะเข้าเงื่อนไขของรูปแบบ pattern</p>
<p><br />
<div style="display:block;clear:both;float:left;height:5px;border-bottom:1px dashed #000000;width:100%;">&nbsp;</div><br />
<strong>ตัวอักขระขีดกลาง หรือเครื่องหมายลบ ( - ) หรือจะเรียกว่า (hyphen) </strong>จะถูกใช้สำหรับกำหนดช่วงของตัวอักษร ตัวอย่างเช่น&nbsp; <br />
<strong>รูปแบบ pattern /[d-m]/<br />
</strong>- หมายถึง ถ้ามีอักขระช่วงตัวอักษร d ถึง m จะเข้าเงื่อนไข pattern คือ ถ้ามีตัว d e f g h i j k l หรือ m</p>
<p>และสามารถใช้เป็นอักขระตรวจสอบ โดยการใช้เครื่องหมาย ( \ )&nbsp; ซึ่งโดยทั่วไปจะไว้ด้านหน้า หรือด้านหลังของ<br />
character class<br />
<strong>ตัวอย่างเช่น&nbsp; <br />
รูปแบบ pattern /[d-m\-]/<br />
</strong>- หมายถึง ถ้ามีอักขระช่วงตัวอักษร d ถึง m หรือเครื่องหมาย - จะเข้าเงื่อนไข pattern <br />
คือ ถ้ามีตัว d e f g h i j k l หรือ m หรือเครื่องหมาย -</p>
<br>
<div style="display:block;clear:both;float:left;height:5px;border-bottom:1px dashed #000000;width:100%;">&nbsp;</div><br />
<p><strong>การกำหนด อักขระ ] ต่อท้ายช่วงของ character class <br />
ตัวอย่างเช่น <br />
รูปแบบ pattern /[W-]46]/<br />
</strong>- จะไม่ได้หมายถึง จากตัว W ถึงเครื่องหมาย } แต่จะหมายถึงตัวอักขระ 2 ตัว คือ<br />
มีตัว W หรือ -&nbsp; แล้วตามด้วย 46]<br />
ดังนั้นข้อความ&nbsp; W46] หรือ -46] จึงเข้าเงื่อนไขตามรูปแบบ pattern ข้างต้น</p>
<p>อยากไรก็ตาม หากมีการตรวจสอบเครื่องหมาย ] ในรูปแบบ pattern จะต้องใช้เครื่องหมาย ( \ ) <br />
<strong>ตัวอย่างเช่น<br />
รูปแบบ pattern /[W-\]46]/</strong></p>
<div style="display:block;clear:both;float:left;height:5px;border-bottom:1px dashed #000000;width:100%;">&nbsp;</div><br />
<p><strong>การใช้งานกับช่วงลำดับของค่า ASCII ยังสามารถใช้สำหรับกำหนดเป็นตัวเลขพิเศษ</strong> <br />
<strong>ตัวอย่างเช่น<br />
รูปแบบ pattern /[\000-\037]/<br />
</strong>- หมายถึง <br />
กำหนดช่วงสำหรับตัวเลขฐาน 8 ช่างค่าตั้งแต่ 000 - 037&nbsp; <br />
ดูตารางค่า ASCII พร้อมค่าในตัวเลขฐาน 8 ได้ที่<br />
<a href="http://www.asciitable.com/">http://www.asciitable.com/</a></p>
<p><strong>ตัวอย่าง<br />
รูปแบบ pattern /[W-c]/i จะมีค่าเท่ากับ รูปแบบ pattern /[][\^_`wxyzabc]/i<br />
</strong>ดูตารางเปรียบเทียบค่า ASCII ได้ที่ <br />
<a href="http://www.asciitable.com/">http://www.asciitable.com/</a></p>
<p>นอกจากเราอาจจะพบว่ามีการใช้งาน&nbsp; backslash character เช่น<br />
\d&nbsp; --&nbsp; ตัวเลขทุกตัว 0 - 9<br />
\D&nbsp; -- ตัวอักขระทุกตัว ยกเว้น เลข 0 - 9<br />
\s&nbsp; --&nbsp; ช่องว่างทุกตัว<br />
\S&nbsp; -- ตัวอักขระทุกตัว ยกเว้นช่องว่าง<br />
\w -- ตัวอักษร ตัวเลข ทุกตัว ยกเว้นตัวอักขระพิเศษ . \ + * ? [ ^ ]&nbsp; % $ ( ) { } = ! &lt; &gt; | : -<br />
\W&nbsp; -- ตัวขระพิเศษทุกตัว และช่องว่าง ยกเว้น ตัวอักษร และตัวเลข</p>
<p>ปรากฏอยู่ใน character class<br />
<strong>ตัวอย่างเช่น <br />
รูปแบบ pattern /[^\W_]/<br />
</strong>- พิจารณาแต่ละตัว <br />
- มีเครื่องหมาย ^ แสดงว่าเป็นค่า invert หรือตรงข้าม<br />
- \W คือ ตัวขระพิเศษทุกตัว และช่องว่าง ยกเว้น ตัวอักษร และตัวเลข ดังนั้น เป็นค่าตรงข้าม จึงหมายถึง <br />
ตัวอักษร ตัวเลข ทุกตัว ยกเว้นตัวอักขระพิเศษ . \ + * ? [ ^ ]&nbsp; % $ ( ) { } = ! &lt; &gt; | : -<br />
- _ คือเครื่องหมาย underscore ดังนั้น เป็นค่าตรงข้าม จังหมายถึง ต้องไม่เป็นเครื่องหมาย underscore&nbsp; ( _ ) <br />
- โดยสรุป คือ เป็น ตัวอักษร ตัวเลข ทุกตัว ยกเว้นตัวอักขระพิเศษ . \ + * ? [ ^ ]&nbsp; % $ ( ) { } = ! &lt; &gt; | : - และ<br />
ไม่เป็นเครื่องหมาย underscore (_)</p>
<p>ตัวอักขระอื่นๆ ที่ไม่ใช่ตัวเลข และตัวอักษร ไม่มีความพิเศษใดๆ ใน character class<br />
หากต้องการตรวจสอบอักขระพิเศษนั้นๆ สามารถใช้เครื่องหมาย ( \ ) กำกับไว้ด้านหน้าได้<br />
ยกเว้น&nbsp; \, -, ^ ที่อยู่ตำแหน่งเริ่มต้น และเครื่องหมาย ]</p>
<div style="display:block;clear:both;float:left;height:5px;border-bottom:1px dashed #000000;width:100%;">&nbsp;</div><br />
<p><strong>ใน Regular Expressions เรายังสามารถใช้งานรูปแบบ POSIX notation <br />
</strong>ใน character class ได้ โดยระบุชื่อ POSIX notation&nbsp; ไว้ในเครื่องหมาย [:&nbsp; :]<br />
<strong>ตัวอย่างเช่น<br />
รูปแบบ pattern /[01[:alpha:]%]/<br />
</strong>- หมายถึงเลข 0 หรือ 1 หรือ ตัวอักษรภาษาอังกฤพิมพ์เล็ก พิมพ์ใหญ่ หรือ % จะเข้าเงื่อนไขของ pattern</p>
<p><strong><br />
class names ที่ใช้ใน Character classes<br />
</strong>alnum&nbsp;&nbsp;&nbsp;&nbsp; ตัวอักษรภาษาอังกฤพิมพ์เล็ก พิมพ์ใหญ่ และตัวเลข<br />
alpha&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ตัวอักษรภาษาอังกฤพิมพ์เล็ก พิมพ์ใหญ่<br />
ascii&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;ตัวอักขระทุกตัว ที่มีค่า ascii code ตั้งแต่ 0 - 127<br />
&nbsp;ตัวอักษรภาษาอังกฤพิมพ์เล็ก พิมพ์ใหญ่ ตัวเลข ตัวอักขระพิเศษ รวมช่องว่าง<br />
blank&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ช่องว่าง หรือว่า แท็บ<br />
cntrl&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; control characters<br />
digit&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ตัวเลขอ 0 - 9 (เหมือนกับการใช้งาน \d)<br />
graph&nbsp;&nbsp;&nbsp;&nbsp; ตัวอักษรภาษาอังกฤพิมพ์เล็ก พิมพ์ใหญ่ ตัวเลข ตัวอักขระพิเศษ ไม่รวมช่องว่าง<br />
lower&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ตัวอักษรภาษาอังกฤพิมพ์เล็กทุกตัว<br />
print&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ตัวอักษรภาษาอังกฤพิมพ์เล็ก พิมพ์ใหญ่ ตัวเลข ตัวอักขระพิเศษ รวมช่องว่าง<br />
punct&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ตัวอักขระพิเศษทุกตัว ไม่รวมตัวอักษรภาษาอังกฤษ และตัวเลข<br />
space&nbsp;&nbsp;&nbsp;&nbsp; ช่องว่าง (ไม่เหมือนกับ \s เลยเสียทีเดียว)<br />
upper&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ตัวอักษรภาษาอังกฤพิมพ์ใหญ่<br />
word&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; กลุ่มคำตัวอักษรภาษาอังกฤษ (เหมือนกับการใช้งาน&nbsp; \w)<br />
xdigit&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ตัวเลขฐานสิบหก</p>
<div style="display:block;clear:both;float:left;height:5px;border-bottom:1px dashed #000000;width:100%;">&nbsp;</div><br />
<p><strong>การกำหนดค่าตรงข้าม ใน character class จะใช้อักขระ&nbsp; ^ <br />
ตัวอย่าง เช่น<br />
รูปแบบ pattern&nbsp; /[12[:^digit:]]/<br />
</strong>- หมายถึง เป็นเลข 1 หรือ 2 หรือ ตัวอักขระทุกตัวที่ไม่ใช่ ตัวเลข 0-9 ยกเว้นเลข 1 กับ 2 ข้างต้น</p>
<p>ตัวอักขระภาษาไทย จะไม่สามารถใช้งานกับการกำหนด pattern ด้วย Character classes แบบ POSIX notation ในลักษณะ [[:&nbsp; :]] ที่กล่าวข้างต้นได้<br />
แต่สามารถใช้ได้ในลักษณะ การกำหนดค่าในเครื่องหมายปีกกาสี่เหลี่ยมได้ เช่น<br />
<strong>ตัวอย่าง เช่น<br />
รูปแบบ pattern&nbsp; /[กขคง]/<br />
</strong>- หมายถึง เป็น ตัวอักษร ก ข ค หรือ ง เป็นต้น</p>]]></description><pubDate>Thu, 03 Feb 2011 16:20:48 +0700</pubDate></item><item><title>การอัพเดท สถานะ facebook อัตโนมัติ ผ่านทางอีเมลล์</title><link>http://www.ninenik.com/การอัพเดท_สถานะ_facebook_อัตโนมัติ_ผ่านทางอีเมลล์-395.html</link><description><![CDATA[<p>facebook ได้ทำการเพิ่มช่องทางในการ อัพโหลดรุปภาพ วีดีโอ หรือสถานะ ไปยังหน้า โพรไฟล์ของเรา<br />
ผ่านทางอีเมลล์</p>
<p>สามารถเข้าไปดูอีเมลล์ของเรา สำหรับใช้งานนี้ได้ โดยให้ทำการล็อกอิน facebook ก่อน แล้วไปที่</p>
<p><a href="http://www.facebook.com/mobile/">http://www.facebook.com/mobile/</a></p>
<p>อีเมลล์ จะมีรูปแบบดังนี้ คือ <a href="mailto:xxxxxx@m.facebook.com">xxxxxx@m.facebook.com</a></p>
<p>xxxxxx คือ ชื่ออีเมลล์เฉพาะของแต่ละคน</p>
<p>(facebook แนะนำว่าไม่ควรนำอีเมลล์ ดังกล่าวนี้ไปบอกใคร ใช้เฉพาะตัวเอง เหตุผลอาจเป็นเพราะ<br />
ถ้ามีคนรู้อีเมลล์นี้ หากส่งอีเมลล์มา ก็จะไปที่แสดงที่หน้า โพรไฟล์เราได้เลย ซึ่งอาจเป็นข้อความ หรือเนื้อหา<br />
ทีไม่พิงประสงค์ อย่างไรก็ตาม คิดว่า facebook น่าจะมีรูปแบบ และการจัดการเกี่ยวกับการใช้อีเมลล์ ในอนาคต)</p>
<p>เมื่อเรามีอีเมลล์ จาก facebook แล้ว เราสามารถทำการ อัพเดทสถานะ ในโพรไฟล์ facebook ของเราได้<br />
โดยการเขียนคำสั่ง php ส่งข้อความไปยังอีเมลล์นั้น</p>
<p>ในที่นี้ จะเสนอตัวอย่างการใช้ php class ส่งอีเมลล์ ตามหัวข้อ</p>
<p><a href="http://www.ninenik.com/ส่งอีเมลล์พร้อมกับแนบไฟล์ด้วย_php_mail_function_ได้อย่างง่าย-125.html">http://www.ninenik.com/ส่งอีเมลล์พร้อมกับแนบไฟล์ด้วย_php_mail_function_ได้อย่างง่าย-125.html</a></p>
<p>ตัวอย่างโค้ด</p>
<pre class="php:controls" name="code">
&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot;
 &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;
&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;
&lt;head&gt;
&lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=utf-8&quot; /&gt;
&lt;title&gt;&lt;/title&gt;
&lt;/head&gt;

&lt;body&gt;
&lt;?php
include(&quot;mimemail.inc.php&quot;);
$mail = new MIMEMAIL(&quot;TEXT&quot;);
$mail-&gt;senderName =&quot;Ninenik.com&quot;; 
$mail-&gt;senderMail =&quot;xxxxxx@gmail.com&quot;; 
$mail-&gt;subject =&quot;ทดสอบการส่งผ่านอีเมลล์ ninenik.com&quot;; 
$mail-&gt;body = '';  
//$mail-&gt;attachment[] = &quot;images/logo_01_Fri.gif&quot;; // ระบุตำแหน่งไฟล์ที่จะแนบ  
$mail-&gt;create();  
if($mail-&gt;send(&quot;xxxxxx@m.facebook.com&quot;)){
	echo &quot;OK&quot;;
}
                
?&gt;                
&lt;/body&gt;
&lt;/html&gt;</pre>
<p>&nbsp;</p>
<p>$mail-&gt;subject =&quot;ทดสอบการส่งผ่านอีเมลล์ ninenik.com&quot;;&nbsp; คือข้อความที่จะแสดง<br />
หากส่งพร้อมกับแนบรูป หัวข้ออีเมลล์ จะส่งไปเป็นคำอธิบายรูป<br />
$mail-&gt;attachment[] = &quot;images/logo_01_Fri.gif&quot;; // ระบุตำแหน่งไฟล์ที่จะแนบ<br />
เมื่อมีการแนบไฟล์ จะเป็นการอัพโหลดรูปนั้น ๆ ไปยัง facebook</p>
<p><br />
นอกจากนี้ เรายังสามารถประยุกต์ ใช้งาน cronjob เพื่อให้ทำการส่งอีเมลล์ข้อมูลไปยัง หน้า<br />
โพรไฟล์ facebook ของเราอัตโนมัติ ตามเวลาที่ต้องการได้</p>
<p>อ่าน การใช้งาน Cronjob ใน Direct Admin Control Panel ตามลิ้งค์ต่อไปนี้<br />
<a href="http://siamhost4u.com/คู่มือการใช้งาน/แสดง/เรื่อง-การใช้งาน,Cronjob,ใน,Direct,Admin,Control,Panel/">http://siamhost4u.com/คู่มือการใช้งาน/แสดง/เรื่อง-การใช้งาน,Cronjob,ใน,Direct,Admin,Control,Panel/</a></p>]]></description><pubDate>Sat, 22 Jan 2011 08:00:00 +0700</pubDate></item><item><title>การใช้งาน Meta characters ใน Regular Expressions ของ PHP</title><link>http://www.ninenik.com/การใช้งาน_Meta_characters_ใน_Regular_Expressions_ของ_PHP-394.html</link><description><![CDATA[<p>Meta-characters ใน Regular Expressions คือ การนำเอาตัวอักขระพิเศษ มาใช้งานในการกำหนด<br />
รูปแบบ pattern ที่มีความซับซ้อน<br />
<strong>โดยจะแบ่ง Meta-characters ออกเป็น 2 ส่วน คือ</strong><br />
1.ส่วนที่ใช้งานในการกำหนดรูปแบบ pattern ในทุกที่ ยกเว้นในเครื่องหมายปีกกาสี่เหลี่ยม [ ]<br />
2.ส่วนที่ใช้งานในการกำหนดรูปแบบ pattern ได้ทั้งข้างในและนอก เครื่องหมายปีกกาสี่เหลี่ยม [ ]</p>
<p><strong>1.ส่วนที่ใช้งานในการกำหนดรูปแบบ pattern ในทุกที่ ยกเว้นในเครื่องหมายปีกกาสี่เหลี่ยม [ ]</strong></p>
<p><strong>\</strong><br />
ใช้เมื่อมีการนำเอา ตัวอักขระพิเศษ มาใช้ในการกำหนดรูปแบบ pattern <br />
ตัวอักขระพิเศษ<br />
<strong>. \ + * ? [ ^ ] $ ( ) { } = ! &lt; &gt; | : -</strong></p>
<p>ตัวอย่างเช่น ต้องการหาว่า มีเครื่องหมาย $ หรือไม่ สามารถกำหนด เป็น<br />
<strong>/\$/</strong></p>
<div style="display:block;clear:both;float:left;height:5px;border-bottom:1px dashed #000000;width:100%;">&nbsp;</div><br />
<p><strong>^<br />
</strong>ใช้เมื่อต้องการตรวจสอบว่า เป็น คำ ตัวอักษร หรือข้อความที่ขึ้นต้นบรรทัด&nbsp; หรือไม่<br />
ซึ่งหมายถึง คำ ตัวอักษร หรือข้อความนั้นๆ ต้องอยู่ข้างหน้าสุด ของบรรทัดเสมอ <br />
( เป็นการขึ้นต้นบรรทัดใหม่จาก \n ไม่ใช่ &lt;br&gt; และไม่มีช่องว่างหน้าคำนั้นๆ)</p>
<p>ตัวอย่างเช่น มีข้อความในตัวแปรอยู่ 3 บรรทัด ได้แก่<br />
Blackpool<br />
Blackburn<br />
Liverpool<br />
ต้องการตรวจสอบว่า คำว่า Black ที่ขึ้นต้นบรรทัด ทุกคำ จะสามารถกำหนดได้เป็น<br />
<strong>/^Black/m</strong></p>
<p>m คือ Pattern Modifiers ใช้สำหรับ ให้ทำการเช็ครูปแบบ pattern ในทุก บรรทัด ที่ขึ้นต้นบรรทัด<br />
ใหม่จาก \n อ่านเพิ่มเติมที่<br />
<a href="http://www.php.net/manual/en/reference.pcre.pattern.modifiers.php">http://www.php.net/manual/en/reference.pcre.pattern.modifiers.php</a></p>
<p>ตัวอย่างการใช้งานกับฟังก์ชัน preg_replace() ใน php โดยเงื่อนไข คือ ให้ทำการขีดเส้นใต้ คำว่า Black ที่ขึ้นต้นบรรทัดทั้งหมด<br />
&nbsp;</p>
<pre class="php:controls" name="code">
&lt;?php
$data_test=&quot;Blackpool Blackhawk
Blackburn
Liverpool&quot;;
echo preg_replace('/(^Black)/m','&lt;u&gt;$1&lt;/u&gt;',$data_test);
?&gt;</pre>
<p><br />
จากตัวอย่างโค้ดด้านบน จะเห็นว่ามีการขีดเส้นใต้คำว่า Black ที่ขึ้นต้นบรรทัด ที่คำว่า Blackpool และ Blackburn ยกเว้นคำว่า Black ใน Blackhawk ที่ไม่ได้ขีดเส้นใต้ ซึ่งไม่เข้าเงื่อนไขของ pattern <br />
เพราะไม่ได้อยู่ขึ้นต้นบรรทัด<br />
ซึ่งกรณีถ้า หากต้องการให้คำว่า Black ทุกคำขึดเส้นใต้ ให้เอา <strong>^</strong> ออก ก็จะได้เป้น<br />
&nbsp;</p>
<pre class="php:controls" name="code">
&lt;?php
$data_test=&quot;Blackpool Blackhawk
Blackburn
Liverpool&quot;;
echo preg_replace('/(Black)/m','&lt;u&gt;$1&lt;/u&gt;',$data_test);
?&gt;</pre>
<div style="display:block;clear:both;float:left;height:5px;border-bottom:1px dashed #000000;width:100%;">&nbsp;</div><br />
<p><strong>$</strong><br />
ใช้เมื่อต้องการตรวจสอบว่า ลงท้ายประโยคหรือบรรทัดด้วย คำ ตัวอักษร หรือข้อความที่กำหนด หรือไม่<br />
ซึ่งหมายถึง คำ ตัวอักษร หรือข้อความนั้นๆ ต้องอยู่ท้ายประโยค ก่อนขึ้นบรรทัดใหม่เสมอ<br />
( เป็นการขึ้นต้นบรรทัดใหม่จาก \n ไม่ใช่ &lt;br&gt; และไม่มีช่องว่างหลังคำนั้นๆ)</p>
<p>ตัวอย่างเช่น มีข้อความในตัวแปรอยู่ 3 บรรทัด ได้แก่<br />
Blackpool<br />
Blackburn<br />
Liverpool<br />
ต้องการตรวจสอบว่า คำว่า pool ที่ลงท้ายประโยคหรือบรรทัด จะสามารถกำหนดได้เป็น<br />
<strong>/pool$/m</strong></p>
<p>m คือ Pattern Modifiers ใช้สำหรับ ให้ทำการเช็ครูปแบบ pattern ในทุก บรรทัด ที่ขึ้นต้นบรรทัด<br />
ใหม่จาก \n อ่านเพิ่มเติมที่<br />
<a href="http://www.php.net/manual/en/reference.pcre.pattern.modifiers.php">http://www.php.net/manual/en/reference.pcre.pattern.modifiers.php</a></p>
<p>ตัวอย่างการใช้งานกับฟังก์ชัน preg_replace() ใน php โดยเงื่อนไข คือ ให้ทำการขีดเส้นใต้ pool ที่ลงท้ายประโยคหรือบรรทัด ทั้งหมด<br />
&nbsp;</p>
<pre class="php:controls" name="code">
&lt;?php
$data_test=&quot;Blackpool Blackhawk
Blackburn
Liverpool&quot;;
echo preg_replace('/(pool$)/m','&lt;u&gt;$1&lt;/u&gt;',$data_test);
?&gt;</pre>
<p><br />
จากตัวอย่างโค้ดด้านบน จะเห็นว่ามีการขีดเส้นใต้คำว่า pool ที่ลงท้ายประโยคหรือบรรทัด ที่คำว่า Liverpool<br />
ยกเว้นคำว่า pool ใน Blackpool ที่ไม่ได้ขีดเส้นใต้ ซึ่งไม่เข้าเงื่อนไขของ pattern เพราะไม่ได้<br />
ลงท้ายประโยคหรือบรรทัดด้วยคำว่า pool<br />
ซึ่งกรณีถ้า หากต้องการให้คำว่า pool ทุกคำขึดเส้นใต้ ให้เอา <strong>$</strong>&nbsp; ออก ก็จะได้เป้น<br />
&nbsp;</p>
<pre class="php:controls" name="code">
&lt;?php
$data_test=&quot;Blackpool Blackhawk
Blackburn
Liverpool&quot;;
echo preg_replace('/(pool)/m','&lt;u&gt;$1&lt;/u&gt;',$data_test);
?&gt;</pre>
<div style="display:block;clear:both;float:left;height:5px;border-bottom:1px dashed #000000;width:100%;">&nbsp;</div><br />
<p><strong>.&nbsp;</strong> (จุด)<br />
ใช้สำหรับตรวจสอบเงื่อนไข แทนตัวอักษรไดก็ได้ ยกเว้นการขึ้นบรรทัดใหม่ (\n ไม่ใช่ &lt;br&gt;)<br />
ตัวอย่างเช่น รูปแบบ pattern คือ</p>
<p><strong>/g..gle/</strong></p>
<p>จะหมายถึงว่า . (จุด) 2 อันสามารถเป็นตัวอักษรอะไรก็ได้<br />
ดังนั้นคำว่า google หรือ geegle หรือ g55gle จึงเข้าเงื่อนไข รูปแบบ pattern ข้างต้น</p>
<div style="display:block;clear:both;float:left;height:5px;border-bottom:1px dashed #000000;width:100%;">&nbsp;</div><br />
<p><strong>[</strong><br />
ใช้สำหรับเป็น ตัวอักขระเริ่มต้น ในการกำหนดการใช้งาน Character classes</p>
<p><br />
<strong>]</strong><br />
เป็นตัวอักขระสำหรับปิด การใช้งานในส่วน Character classes</p>
<p><strong>ตัวอย่างการใช้งาน ตัวอักขระเปิด และปิด สำหรับ Character classes</strong><br />
รูปแบบ pattern <strong>/[online]/</strong><br />
- หมายถึงตัวอักษรทุกตัว ใน [] จะเข้าเงื่อนไข คือเป็นตัว o n l i หรือ e ซึ่งจะอยู่ตรงไหนก็ได้<br />
โดยไม่ได้หมายถึงว่าต้องเป็น คำว่า online ถึงจะเข้าเงื่อนไข <br />
(การใช้งานเกี่ยวกับ Character classes จะได้นำมาอธิบายต่อๆไป)</p>
<div style="display:block;clear:both;float:left;height:5px;border-bottom:1px dashed #000000;width:100%;">&nbsp;</div><br />
<p><strong>|</strong><br />
ใช้สำหรับกำหนดทางเลือก ให้กับรูปแบบ pattern<br /><br>
<strong>ตัวอย่างเช่น </strong><br />
รูปแบบ pattern<strong> /(.gif)|(.png)|(.jpg)|(.jpeg)/</strong><br />
- หมายถึง ข้อความว่า .gif หรือ .png หรือ .jpg หรือ .jpeg จะเข้าเงื่อนไข</p>
<div style="display:block;clear:both;float:left;height:5px;border-bottom:1px dashed #000000;width:100%;">&nbsp;</div><br />
<p><strong>(</strong><br />
เป็นตัวอักขระเริ่มต้น เปิดใช้ ส่วนการกำหนดกลุ่ม pattern ย่อย</p>
<p><strong>)<br />
</strong>เป็นตัวอักขระปิดการใช้ ส่วนการกำหนดกลุ่ม pattern ย่อย</p>
<p><strong>ตัวอย่างการใช้งานตัวอักขระเปิดและ ปิดการใช้ ส่วนการกำหนดกลุ่ม pattern ย่อย<br />
ตัวอย่างเช่น </strong><br />
รูปแบบ pattern <strong>/(.gif)|(.png)|(.jpg)|(.jpeg)/</strong><br />
- จะเห็นว่าใน รูปแบบ pattern จะมี subpattern อยู่ในวงเล็ม () ซึ่งมีทั้งหมด 4 subpattern<br />
คือ (.gif) (.png) (.jpg) และ (.jpeg)</p>
<div style="display:block;clear:both;float:left;height:5px;border-bottom:1px dashed #000000;width:100%;">&nbsp;</div><br />
<p><strong>?</strong><br />
เป็นตัวอักขระ สำหรับกำหนดจำนวนตัวอักษรใช้ในกรณี<br />
หมายถึง มี 1 ตัวหรือไม่มีก็ได้</p>
<p><strong>ตัวอย่างเช่น</strong><br />
รูปแบบ pattern<strong> /.jpe?g/</strong><br />
- หมายถึง มีตัว e 1 ตัว หรือไม่มีก็ได้ ดังนั้น .jpg และ .jpeg จึงเงื่อนไข<br />
รูปแบบ pattern<strong> /bi.?d/<br />
</strong>- หมายถึง มีตัวอักษรอะไรก็ได้ 1 ตัว หรือไม่มีก็ได้ ต่อจากคำว่า bi และต่อด้วยตัว d<br />
ดังนั้น <br />
bid ก็จะเข้าเงื่อนไข เพราะ ไม่มีตัวที่่ต่อจาก bi<br />
bird ก็จะเข้าเงื่อนไข เพราะ มีตัว r 1 ตัว ต่อจากคำว่า bi<br />
birod จะไม่เข้าเงื่อนไข เพราะ มีตัว r และ o ต่อจากคำว่า bi ซึ่งรวมกันมากกว่า 1 ตัว</p>
<div style="display:block;clear:both;float:left;height:5px;border-bottom:1px dashed #000000;width:100%;">&nbsp;</div><br />
<p><br />
<strong>*</strong><br />
เป็นตัวอักขระ สำหรับกำหนดจำนวนตัวอักษรใช้ในกรณี<br />
หมายถึง ไม่มี หรือมีก็ตัวก็ได้</p>
<p><strong>ตัวอย่างเช่น </strong><br />
รูปแบบ pattern <strong>/g*gle/</strong><br />
- หมายถึง มีตัว g หรือไม่มีก็ได้ และต่อด้วยคำว่า gle<br />
ดังนั้น<br />
google จึงเข้าเงื่อนไข เพราะมีตัว g ก่อน แล้วมีการต่อด้วย gle<br />
gggle จึงเข้าเงื่อนไข เพราะมีตัว g ก่อนกี่ตัวก็ได้ แล้วมีการต่อด้วย gle<br />
oogle จึงเข้าเงื่อนไข เพราะไม่มีตัว g แล้วมีการต่อด้วย gle<br />
single จึงเข้าเงื่อนไข เพราะไม่มีตัว g แล้วมีการต่อด้วย gle</p>
<div style="display:block;clear:both;float:left;height:5px;border-bottom:1px dashed #000000;width:100%;">&nbsp;</div><br />
<p><strong>+</strong><br />
เป็นตัวอักขระ สำหรับกำหนดจำนวนตัวอักษรใช้ในกรณี<br />
หมายถึง จะมีกี่ตัวก็ได้ไม่จำกัด แต่ต้องมีอย่างน้อย 1 ตัว</p>
<p><strong>ตัวอย่างเช่น </strong><br />
รูปแบบ pattern<strong> /g+gle/</strong><br />
- หมายถึง มีตัว g อย่างน้อย 1 ตัวหรือมากกว่า แล้วต้องต่อด้วย gle เลย<br />
ดังนั้น<br />
google จึงไม่เข้าเงื่อนไข แม้มีตัว g ก่อน อย่างน้อย 1 ตัว แต่ต่อ oo ก่อนที่จะต่อด้วย gle<br />
gggle จึงเข้าเงื่อนไข เพราะมีตัว g ก่อน อย่างน้อย 1 ตัว แล้วมีการต่อด้วย gle<br />
oogle จึงไม่เข้าเงื่อนไข เพราะไม่มีตัว g ก่อน อย่างน้อย 1 ตัว ก่อนที่จะต่อด้วย gle</p>
<div style="display:block;clear:both;float:left;height:5px;border-bottom:1px dashed #000000;width:100%;">&nbsp;</div><br />
<p><br />
<strong>{</strong><br />
เป็นอักขระสำหรับ เปิด การกำหนดจำนวนตัวอักษร ต่ำสุุด และมากสุด ที่ต้องมี</p>

<p><strong>}</strong><br />
เป็นอักขระสำหรับ ปิด การกำหนดจำนวนตัวอักษร ต่ำสุุด และมากสุด ที่ต้องมี</p>
<p><strong>ตัวอย่างเช่น มีข้อความ 3 ข้อความ</strong><br />
google<br />
gooogle<br />
gooooogle</p>
<p>รูปแบบ pattern <strong>/go{1}gle/</strong><br />
- หมายถึง มีตัว g ตามด้วยตัว o 1 ตัวแล้วต่อด้วย gle<br />
google ไม่เข้าเงื่อนไข เพราะ มีตัว o 2 ตัว<br />
gooogle ไม่เข้าเงื่อนไข เพราะ มีตัว o 3 ตัว<br />
gooooogle ไม่เข้าเงื่อนไข เพราะ มีตัว o 5 ตัว</p>
<p>รูปแบบ pattern<strong> /go{2,4}gle/<br />
</strong>- หมายถึง มีตัว g ตามด้วยตัว o อย่างน้อย 2 ตัวแต่ไม่เกิน 4 ตัว แล้วต่อด้วย gle<br />
google เข้าเงื่อนไข เพราะ มีตัว o 2 ตัว อยู่ ระหว่าง 2 ถึง 4 ตัว<br />
gooogle เข้าเงื่อนไข เพราะ มีตัว o 3 ตัว อยู่ ระหว่าง 2 ถึง 4 ตัว<br />
gooooogle ไม่เข้าเงื่อนไข เพราะ มีตัว o 5 ตัว เกิน 4 ตัว</p>
<br>
<br>
<br>

<p><strong>2.ส่วนที่ใช้งานในการกำหนดรูปแบบ pattern ได้ทั้งข้างในและนอก เครื่องหมายปีกกาสี่เหลี่ยม [ ]</strong></p>
<p><strong>\</strong><br />
ใช้เมื่อมีการนำเอา ตัวอักขระพิเศษ มาใช้ในการกำหนดรูปแบบ pattern <br />
ตัวอักขระพิเศษ<br />
<strong>. \ + * ? [ ^ ] $ ( ) { } = ! &lt; &gt; | : -</strong></p>
<p>ตัวอย่างเช่น ต้องการหาว่า มีเครื่องหมาย $ หรือไม่ สามารถกำหนด เป็น<br />
<strong>/[\$]/</strong></p>
<div style="display:block;clear:both;float:left;height:5px;border-bottom:1px dashed #000000;width:100%;">&nbsp;</div><br />
<p><strong>^</strong><br />
กำหนดตรงกันข้ามกับ character class โดย กำหนดไว้ด้านหน้าเท่านั้น<br />
ตัวอย่าง รูปแบบ pattern<br />
<strong>/[^aioue]/</strong><br />
- หมายถึง ตัวอักขระทุกตัว ที่ไม่ใช่ ตัว a i o u และ e <br />
ซึ่งตรงข้ามกับ รูปแบบ pattern<br />
<strong>/[aioue]/</strong><br />
- หมายถึง ตัวอักขระที่เป็นตัว a i o u และ e</p>
<div style="display:block;clear:both;float:left;height:5px;border-bottom:1px dashed #000000;width:100%;">&nbsp;</div><br />
<p><strong>-</strong><br />
ใช้สำหรับกำหนด ช่วงของการใช้งาน character class</p>
<p><strong>ตัวอย่างเช่น <br />
</strong>[a-z] หมายถึง ตัวอักษร a ถึง&nbsp; z ตัวพิมพ์เล็ก<br />
[A-Z] หมายถึง ตัวอักษร A ถึง Z ตัวพิมพ์ใหญ่<br />
[0-9] หมายถึง ตัวเลข 0 - 9</p>
<div style="display:block;clear:both;float:left;height:5px;border-bottom:1px dashed #000000;width:100%;">&nbsp;</div><br />
<p><strong>]</strong><br />
ส่วนหรับปิดการใช้งาน character class<br />
ตัวอย่าง รูปแบบ pattern<br />
<strong>/[[:space:]]/</strong><br />
- หมายถึงช่องว่างทุกตัว</p>
<p><br />
รายละเอียด ในแต่ละส่วน จะได้แนะนำในโอกาสต่อๆ ไป</p>
<p>&nbsp;</p>]]></description><pubDate>Fri, 21 Jan 2011 01:49:39 +0700</pubDate></item><item><title>อักขระคั่น (Delimiters) ในการใช้งาน Regular Expressions ของ PHP</title><link>http://www.ninenik.com/อักขระคั่น_(Delimiters)_ในการใช้งาน_Regular_Expressions_ของ_PHP-393.html</link><description><![CDATA[<p>เมื่อมีการใช้งาน ฟังก์ชัน Regular Expressions รูปแบบ pattern จะต้องปิดด้วย อักขระคั่นเสมอ<br />
โดยอักขระคั่นที่สามารถกำหนดได้ เป็นดังนี้<br />
- ไม่เป็นตัวอักษร<br />
- ไม่เป็น&nbsp; เครื่องหมาย&nbsp; backslash&nbsp; ( \ )<br />
- ไม่เป็นช่องว่าง</p>
<p>โดยทั่วไปจะใช้เครื่องหมาย forward slashes ( / ) ,hash signs (#) และ tildes (~)</p>
<p>ตัวอย่างรูปแบบการใช้งาน อักขระคั่น ที่มีรูปแบบถูกต้อง</p>
<pre class="php:controls" name="code">
/foo bar/   --- ใช้ /
#^[^0-9]$#   --- ใช้ #
+php+   --- ใช้ +
%[a-zA-Z0-9_-]%  --- ใช้ %</pre>
<p>ในกรณีมีการใช้ตัวอักขระคั่นใน pattern สามารถเลี่ยงการเกิด error โดยการใช้เครื่องหมาย backslash ( \ ) เช่น</p>
<p>มีการใช้ตัวอักขระคั่นคือ /&nbsp; กับคำว่า http://<br />
รูปแบบ pattern ที่ยังไม่ถูกต้อง จะเป็น</p>
<p><strong>/http:/// </strong></p>
<p>จะเห็นว่ามีการใช้ / ซี่งเป็นอักขระคั่นใน pattern คือในข้อความว่า http:// ดังนั้น เพื่อให้รูปแบบ pattern ถูกต้อง<br />
จึงต้องใช้เครื่องหมาย backslash ( \ )&nbsp; <br />
จะได้รูปแบบ pattern ที่ถูกต้องดังนี้คือ</p>
<p><strong>/http:\/\//</strong></p>
<p>ได้จากการเปลี่ยน // ใน pattern เป็น \/\/</p>
<p>หรือจะใช้วิธีการเปลี่ยนตัวอักขระคั่นอื่นแทน เพื่อให้อ่านได้ง่ายขึ้น เช่นใช้เครื่องหมาย hash sign ( # ) แทน<br />
จะได้เป็น</p>
<p>#http://#</p>
<p>นอกจากนั้น ยังสามารถใช้ฟังก์ชัน preg_quote() ใน php เพื่อหลีกเลี่ยงปัญหา การกำหนด ตัวอักขระพิเศษใน<br />
pattern ซื่งได้แก่&nbsp;</p>
<p><strong>. \ + * ? [ ^ ] $ ( ) { } = ! &lt; &gt; | : -</strong></p>
<p>รวมถึงตัวอักขระคั่น ที่ใช้ใน pattern ด้วย</p>
<p><strong>ตัวอย่าง</strong></p>
<pre class="php:controls" name="code">
&lt;?php
$data_test2=&quot;http://www.google.com&quot;;
$my_pattern=&quot;http://&quot;;
$my_pattern=preg_quote($my_pattern,'/');
echo preg_match('/'.$my_pattern.'/',$data_test2); // ผลลัพธ์ เท่ากับ 1
// กรณีรูปแบบไม่ซับซ้อน เราสามารถเขียนเข้าไปในส่วนใช้งานโดยตรงได้
// โดยตัดบรรทัดที่ 3 กับ 4 ออก
// แล้วเปลี่บน บรรทัดที่ 5 เป็น
// echo preg_match('/http\:\/\//',$data_test2); // ผลลัพธ์ เท่ากับ 1
?&gt;</pre>
<p>อธิบาย<br />
จากตัวอย่าง ต้องการหาว่า ตัวแปร $data_test2 มีรูปแบบข้อความ http:// <br />
ตามตัวแปร $my_pattern อยู่หรือไม่</p>
<p>แต่เนื่องจากตัวแปร $my_pattern มีตัวขระพิเศษ คือ : (colon) และตัวอักขระคั่น คือ //<br />
อยู่ จึงจำเป็นต้องกำหนดรูปแบบ pattern ให้ถูกต้องก่อน</p>
<p>โดยการใช้ ฟังก์ชัน preg_quote ตามโค้ดด้านบน&nbsp; บรรทัดที่ 4 <br />
ซึ่งจะทำให้ได้รูปแบบ pattern&nbsp; เป็น <strong>http://</strong></p>
<p>เมื่อทำการทดสอบ จะได้ผลลัพธ์เท่ากับ 1 คือหมายถึง มีคำว่า http:// อยุ่<br />
หากต้องการทดสอบว่า ถ้าไม่ใช้คำสั่ง preg_quote จะมีผลอย่างไร? ให้ทำการ comment บรรทัดที่ 4<br />
แล้วทดสอบ รันผลลัพธ์ดู</p>
<p>เราสามารถกำหนด เงื่อนไขการตรวจสอบรูปแบบ pattern โดยการเพิ่ม pattern modifiers เช่น<br />
ตัวอย่างเบื้องต้นในที่นี้ คือตัวอักษร i เงื่อนไขคือ เป็นตัวพิมพ์เล็ก หรือตัวพิมพ์ใหญ่ ก็ได้ ซึ่งจะได้<br />
รูปแบบ pattern ดังนี้</p>
<p><strong>/http\:\/\//i</strong></p>
<p><strong>ตัวอย่างการใช้งานใน php</strong></p>
<pre class="php:controls" name="code">
&lt;?php
$data_test2=&quot;HTTP://www.google.com&quot;;
echo preg_match('/http\:\/\//i',$data_test2);  // ผลลัพธ์ เท่ากับ 1
?&gt;</pre>
<p><br />
สามารถศึกษาเพิ่มเติมได้ด้วยตัวเองที่ <br />
<a href="http://www.php.net/manual/en/regexp.introduction.php">http://www.php.net/manual/en/regexp.introduction.php</a></p>
<p><br />
<strong>หมายเหตุ:: <br />
</strong>เนื้อหาข้างต้นเป็นการอธิบายตามความเข้าใจของผู้เขียน ซึ่งเพิ่งจะศึกษาเกี่ยวกับ<br />
Regular Expressions ของ PHP&nbsp; โดยอ้างอิงจากลิ้งค์</p>
<p><a href="http://www.php.net/manual/en/regexp.reference.delimiters.php">http://www.php.net/manual/en/regexp.reference.delimiters.php</a></p>
<p>ดังนั้น เพื่อความเข้าใจที่ถูกต้อง แนะนำให้ศึกษาจาก ลิ้งค์ดังกล่าวโดยตรง<br />
ส่วนใคร ที่ต้องการศึกษาไปพร้อมๆ กับผู้เขียน ก็ติดตามเนื้อหา เกี่ยวกับ <br />
Regular Expressions ของ PHP ได้ที่นี้ <a href="http://www.ninenik.com/">www.ninenik.com</a> ได้เลย</p>]]></description><pubDate>Wed, 19 Jan 2011 16:15:48 +0700</pubDate></item><item><title>แนะนำ PHP Simple HTML DOM Parser สำหรับ ดึงข้อมูล เฉพาะส่วนที่ต้องการ</title><link>http://www.ninenik.com/แนะนำ_PHP_Simple_HTML_DOM_Parser_สำหรับ_ดึงข้อมูล_เฉพาะส่วนที่ต้องการ-388.html</link><description><![CDATA[<p>php class ตัวนี้ เหมาะสำหรับใช้งาน ในการค้นหา และดึงข้อมูล จาก แท็ก HTML เฉพาะส่วนที่ต้องการ เช่น</p>
<pre class="php:controls" name="code">
// หาลิ้งค์ทั้งหมดใน แท็ก HTML คืนค่ากลับมาเป็นตัวแปร array
$ret = $html-&gt;find('a');

// หาลิ้งค์ ลำดับที่ต้องการใน แท็ก HTML คืนค่าเป็น Object  ที่ค้นเจอ หากไม่มีคืนค่าเป็น null
$ret = $html-&gt;find('a', 0); // ลำดับตัวแรกเริ่มต้นที่เลข 0 (zero based)

// หาแท็ก div ที่มี id=foo ทั้งหมดใน HTML คืนค่ากลับมาเป็นตัวแปร array
$ret = $html-&gt;find('div[id=foo]'); // &lt;div id=&quot;foo&quot;&gt;&lt;/div&gt;

// หาแท็ก div ทั้งหมดใน HTML ที่มี attribute ชื่อ id
$ret = $html-&gt;find('div[id]');

// หาแท็กใดก็ได้ที่มี attribute ชื่อ id
$ret = $html-&gt;find('[id]');</pre>
<p>อ่านวิธีการใช้งานแบบละเอียดเพิ่มเติมได้ด้วยตัวเองได้ที่ <br />
<a href="http://simplehtmldom.sourceforge.net/manual.htm">http://simplehtmldom.sourceforge.net/manual.htm</a></p>
<p>ดาวน์โหลดไฟล์ php class สำหรับใช้งานได้ที่ <br />
<a href="http://sourceforge.net/projects/simplehtmldom/files/">http://sourceforge.net/projects/simplehtmldom/files/</a></p>
<p>แนะนำให้โหลดเก็บไว้ใช้งาน รวมทั้งบันทึกวิธีการใช้งานไว้ด้วย</p>
<p><strong>คู่มือ</strong></p>

<link type="text/css" rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.5/themes/south-street/jquery-ui.css" />
<style type="text/css"> 
.ui-tabs{
	font-family:tahoma;
	font-size:11px;
}
</style>


<h1 align="center"><a name="top"></a><a href="index.htm">PHP Simple HTML DOM Parser</a></h1>
<div id="content">
  <h2>Index</h2>
  <ul>
    <li><a href="#section_create">How to create HTML DOM object?</a></li>
    <li><a href="#section_find">How to find HTML elements?</a></li>
    <li><a href="#section_access">How to access the HTML element's attributes?</a> </li>
    <li><a href="#section_traverse">How to traverse the DOM tree?</a></li>
    <li><a href="#section_dump">How to dump contents of DOM object?</a></li>
    <li><a href="#section_callback">How to customize the parsing behavior?</a></li>
    <li><a href="http://simplehtmldom.sourceforge.net/manual_api.htm">API Reference</a></li>
    <li><a href="http://simplehtmldom.sourceforge.net/manual_faq.htm">FAQ</a></li>
  </ul>
  <a class="top" href="#top">Top</a>
  <a name="section_create"></a>
<h2>How to create HTML DOM object?</h2>
  <a class="top" href="#top">Top</a>
  <div id="container_create">
    <ul>
      <li><a href="#frag_create_quick"><span>Quick way</span></a></li>
      <li><a href="#frag_create_oo"><span>Object-oriented way</span></a></li>
    </ul>
    <div id="frag_create_quick">
      <div class="code"><span class="comment">// Create a DOM object from a string</span><br>
    $html = <strong>str_get_html</strong>(<span class="var">'&lt;html&gt;&lt;body&gt;Hello!&lt;/body&gt;&lt;/html&gt;'</span>);<br>
    <br>
    <span class="comment">// Create a DOM object from a URL</span><br>
    $html = <strong>file_get_html</strong>(<span class="var">'http://www.google.com/'</span>);<br>
    <br>
    <span class="comment">// Create a DOM object from a HTML file</span><br>
    $html = <strong>file_get_html</strong>(<span class="var">'test.htm'</span>);<span class="comment"><br>
  </span></div>
    </div>
    <div id="frag_create_oo">
      <div class="code"><span class="comment">// Create a DOM object</span><br>
    $html = new <strong>simple_html_dom</strong>();<br>
    <br>
    <span class="comment">// Load HTML from a string</span><br>
    $html-&gt;<strong>load</strong>(<span class="var">'&lt;html&gt;&lt;body&gt;Hello!&lt;/body&gt;&lt;/html&gt;'</span>);<br>
    <br>
    <span class="comment">// Load HTML from a URL </span> <br>
    $html-&gt;<strong>load_file</strong>(<span class="var">'http://www.google.com/'</span>);<br>
    <br>
    <span class="comment">// Load HTML from a HTML file</span> <br>
    $html-&gt;<strong>load_file</strong>(<span class="var">'test.htm'</span>);</div>
    </div>
  </div>
  <a name="section_find"></a>
  <h2>How to find HTML elements?</h2>
  <a class="top" href="#top">Top</a>
  <div id="container_find">
    <ul>
      <li><a href="#frag_find_basic"><span>Basics</span></a></li>
      <li><a href="#frag_find_advanced"><span>Advanced</span></a></li>
      <li><a href="#frag_find_chain"><span>Descendant selectors</span></a></li>
      <li><a href="#frag_find_nested"><span>Nested selectors</span></a></li>
      <li><a href="#frag_find_attr"><span>Attribute Filters</span></a></li>
      <li><a href="#frag_find_textcomment"><span>Text &amp; Comments</span></a></li>
    </ul>
    <div id="frag_find_basic">
      <div class="code"> <span class="comment">// Find all <strong>anchors</strong>, returns a <strong>array</strong> of element objects</span><br>
    $ret = $html-&gt;find(<span class="var">'<strong>a</strong>'</span>);<br>
    <br>
    <span class="comment">// Find <strong>(N)th</strong> <strong>anchor</strong>, returns element object or <strong>null</strong> if not found</span> <span class="comment">(zero based)</span><br>
    $ret = $html-&gt;find(<span class="var">'<strong>a</strong>', <strong>0</strong></span>);<br>
    <br>
    <span class="comment">// Find all <strong>&lt;div&gt;</strong> which attribute <strong>id=foo</strong></span><br>
    $ret = $html-&gt;find(<span class="var">'<strong>div[id=foo]</strong>'</span>); <br>
    <br>
    <span class="comment">// Find all <strong>&lt;div&gt;</strong> with the <strong>id</strong> attribute</span><br>
    $ret = $html-&gt;find(<span class="var">'<strong>div[id]</strong>'</span>);<br>
    <br>
    <span class="comment">// Find all element has attribute<strong> id</strong></span><br>
    $ret = $html-&gt;find(<span class="var">'<strong>[id]</strong>'</span>);<br>
      </div>
    </div>
    <div id="frag_find_advanced">
      <div class="code"><span class="comment">// Find all element which <strong>id</strong>=foo</span><br>
    $ret = $html-&gt;find(<span class="var">'<strong>#foo</strong>'</span>);<br>
    <br>
    <span class=comment>// Find all element which <strong>class</strong>=foo</span><br>
    $ret = $html-&gt;find(<span class=var>'<strong>.foo</strong>'</span>);<br>
    <br>
    <span class="comment">// Find all <strong>anchors</strong> and <strong>images</strong> </span><br>
    $ret = $html-&gt;find(<span class="var">'<strong>a, img</strong>'</span>); <br>
    <br>
    <span class="comment">// Find all <strong>anchors</strong> and <strong>images</strong> with the "title" attribute</span><br>
    $ret = $html-&gt;find(<span class="var">'<strong>a[title], img[title]</strong>'</span>);<br>
      </div>
    </div>
    <div id="frag_find_attr">
      <div class="code"> Supports these operators in attribute selectors:<br>
          <br>
          <table cellpadding="1" cellspacing="1">
            <tr>
              <th width="25%">Filter</th>
              <th width="75%">Description</th>
            </tr>
            <tr>
              <td>[attribute]</td>
              <td>Matches elements that <strong>have</strong> the specified attribute.</td>
            </tr>
            <tr>
              <td>[attribute=value]</td>
              <td>Matches elements that have the specified attribute with a <strong>certain value</strong>.</td>
            </tr>
            <tr>
              <td>[attribute!=value]</td>
              <td>Matches elements that <strong>don't have</strong> the specified attribute with a certain value.</td>
            </tr>
            <tr>
              <td>[attribute^=value]</td>
              <td>Matches elements that have the specified attribute and it <strong>starts</strong> with a certain value.</td>
            </tr>
            <tr>
              <td>[attribute$=value]</td>
              <td>Matches elements that have the specified attribute and it <strong>ends</strong> with a certain value.</td>
            </tr>
            <tr>
              <td>[attribute*=value]</td>
              <td>Matches elements that have the specified attribute and it <strong>contains</strong> a certain value.</td>
            </tr>
          </table>
      </div>
    </div>
    <div id="frag_find_chain">
      <div class="code"><span class="comment">// Find all &lt;li&gt; in &lt;ul&gt; </span><br>
    $es = $html-&gt;find(<span class="var">'<strong>ul li</strong>'</span>);<br>
    <br>
    <span class="comment">// Find Nested &lt;div&gt; </span><span class="comment">tags</span><br>
    $es = $html-&gt;find(<span class="var">'<strong>div div div</strong>'</span>); <br>
    <br>
    <span class="comment">// Find all &lt;td&gt; in &lt;table&gt; which class=hello </span><br>
    $es = $html-&gt;find(<span class="var">'<strong>table.hello td</strong>'</span>);<br>
    <br>
    <span class="comment">// Find all td tags with attribite align=center in table tags </span><br>
    $es = $html-&gt;find(<span class="var">''<strong>table</strong><strong> td[align=center]</strong>'</span>);<br>
      </div>
    </div>
    <div id="frag_find_textcomment">
      <div class="code"><span class="comment"> // Find all text blocks </span><br>
    $es = $html-&gt;find(<span class="var">'<strong>text</strong>'</span>);<br>
    <br>
    <span class="comment">// Find all comment (&lt;!--...--&gt;) blocks </span><br>
    $es = $html-&gt;find(<span class="var">'<strong>comment</strong>'</span>);<br>
      </div>
    </div>
    <div id="frag_find_nested">
      <div class="code"> <span class="comment">// Find all &lt;li&gt; in &lt;ul&gt; </span><br>
    foreach($html-&gt;find(<span class="var">'<strong>ul</strong>'</span>) as $ul) <br>
    {<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; foreach($ul-&gt;find(<span class="var">'<strong>li</strong>'</span>) as $li) <br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span class="comment">// do something...</span><br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>
    }<br>
    <br>
    <span class="comment">// Find first &lt;li&gt; in first &lt;ul&gt;</span> <br>
    $e = $html-&gt;find(<span class="var">'<strong>ul</strong>', <strong>0</strong></span>)-&gt;find(<span class="var">'<strong>li</strong>', <strong>0</strong></span>);<br>
      </div>
    </div>
  </div>
  <a name="section_access"></a>
  <h2>How to access the HTML element's attributes?</h2>
  <a class="top" href="#top">Top</a>
  <div id="container_access">
    <ul>
      <li><a href="#frag_access_attr"><span>Get, Set and Remove attributes</span></a></li>
      <li><a href="#frag_access_special"><span>Magic attributes</span></a></li>
      <li><a href="#frag_access_tips"><span>Tips</span></a></li>
    </ul>
    <div id="frag_access_attr">
      <div class="code"> <span class="comment">// <strong>Get</strong> a attribute ( If the attribute is <strong>non-value</strong> attribute (eg. checked, selected...), it will returns <strong>true</strong> or <strong>false</strong>)</span><br>
    $value = $e-&gt;<strong>href</strong>;<br>
    <br>
    <span class="comment">// <strong>Set</strong> a attribute(If the attribute is <strong>non-value</strong> attribute (eg. checked, selected...), set it's value as <strong>true</strong> or <strong>false</strong>)</span><br>
    $e-&gt;<strong>href</strong> = <span class="var">'my link'</span>;<br>
    <br>
    <span class="comment">// <strong>Remove</strong> a attribute, set it's value as null! </span><br>
    $e-&gt;<strong>href</strong> = <strong><span class="var">null</span></strong>;<br>
    <br>
    <span class="comment">// <strong>Determine</strong> whether a attribute exist?</span> <br>
    if(isset($e-&gt;<strong>href</strong>)) <br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo <span class="var">'href exist!'</span>;<br>
      </div>
    </div>
    <div id="frag_access_special">
      <div class="code"> <span class="comment">// Example</span><br>
          <span class="hl-var">$</span><span class="hl-code">html = </span>str_get_html<span class="hl-brackets">(</span><span class="var">&quot;&lt;div&gt;foo &lt;b&gt;bar&lt;/b&gt;&lt;/div&gt;&quot;</span><span class="hl-brackets">)</span><span class="hl-code">;</span> <br>
    $e = $html-&gt;find(<span class="var">&quot;div&quot;</span>, <span class="var">0</span>);<br>
    <br>
    echo $e-&gt;<strong>tag</strong>; <span class="comment">// Returns: &quot; <strong>div</strong>&quot;</span><br>
    echo $e-&gt;<strong>outertext</strong>; <span class="comment">// Returns: &quot; <strong>&lt;div&gt;foo &lt;b&gt;bar&lt;/b&gt;&lt;/div&gt;</strong>&quot;</span><br>
    echo $e-&gt;<strong>innertext</strong>; <span class="comment">// Returns: &quot; <strong>foo &lt;b&gt;bar&lt;/b&gt;</strong>&quot;</span><br>
    echo $e-&gt;<strong>plaintext</strong>; <span class="comment">// Returns: &quot; <strong>foo </strong><strong>bar</strong>&quot;<br>
    <br>
    </span>
    <table cellspacing="1" cellpadding="1">
      <tr bgcolor="#CCCCCC">
        <th width="25%">Attribute Name</th>
        <th width="75%">Usage</th>
      </tr>
      <tr>
        <td>$e-&gt;<strong>tag</strong></td>
        <td>Read or write the <strong>tag name</strong> of element.</td>
      </tr>
      <tr>
        <td>$e-&gt;<strong>outertext</strong></td>
        <td>Read or write the <strong>outer HTML text </strong> of element.</td>
      </tr>
      <tr>
        <td>$e-&gt;<strong>innertext</strong></td>
        <td>Read or write the <strong>inner HTML text </strong> of element.</td>
      </tr>
      <tr>
        <td>$e-&gt;<strong>plaintext</strong></td>
        <td>Read or write the <strong>plain text </strong> of element.</td>
      </tr>
    </table>
      </div>
    </div>
    <div id="frag_access_tips">
      <div class="code"><span class="comment">// <strong>Extract</strong> contents from HTML </span><br>
    echo <strong>$html</strong>-&gt;<strong>plaintext</strong>;<br>
    <br>
    <span class="comment"> // <strong>Wrap</strong> a element</span><br>
    $e-&gt;<strong>outertext</strong> = <span class="var">'&lt;div class=&quot;wrap&quot;&gt;'</span> . $e-&gt;<strong>outertext</strong> . <span class="var">'&lt;div&gt;</span>';<br>
    <br>
    <span class="comment">// <strong>Remove</strong> a element, set it's outertext as an empty string </span><br>
    $e-&gt;<strong>outertext</strong> = <span class="var">''</span>;<br>
    <br>
    <span class="comment">// <strong>Append</strong> a element</span><br>
    $e-&gt;<strong>outertext</strong> = $e-&gt;<strong>outertext</strong> . <span class="var">'&lt;div&gt;foo</span><span class="var">&lt;div&gt;</span>';<br>
    <br>
    <span class="comment">// <strong>Insert</strong> a element</span><br>
    $e-&gt;<strong>outertext</strong> = <span class="var">'&lt;div&gt;foo</span><span class="var">&lt;div&gt;</span>' . $e-&gt;<strong>outertext</strong>;<br>
      </div>
    </div>
  </div>
  <a name="section_traverse"></a>
  <h2>How to traverse the DOM tree?</h2>
  <a class="top" href="#top">Top</a>
  <div id="container_traverse">
    <ul>
      <li><a href="#frag_traverse_background"><span>Background Knowledge</span></a></li>
      <li><a href="#frag_traverse_traverse"><span>Traverse the DOM tree</span></a></li>
    </ul>
    <div id="frag_traverse_background">
      <div class="code"> <span class="comment">// If you are not so familiar with HTML DOM, check this <a href="http://php.net/manual/en/book.dom.php" target="_blank"><span class="var">link</span></a> to learn more... </span><br>
          <br>
          <span class="comment">// Example</span><br>
    echo $html-&gt;<strong>find</strong>(<span class="var">&quot;#div1&quot;, 0</span>)-&gt;<strong>children</strong>(<span class="var">1</span>)-&gt;<strong>children</strong>(<span class="var">1</span>)-&gt;<strong>children</strong>(<span class="var">2</span>)-&gt;<span class="var">id</span>;<br>
    <span class="comment">// or</span> <br>
    echo $html-&gt;<strong>getElementById</strong>(<span class="var">&quot;div1&quot;</span>)-&gt;<strong>childNodes</strong>(<span class="var">1</span>)-&gt;<strong>childNodes</strong>(<span class="var">1</span>)-&gt;<strong>childNodes</strong>(<span class="var">2</span>)-&gt;<strong>getAttribute</strong>(<span class="var">'id'</span>); </div>
    </div>
    <div id="frag_traverse_traverse">
      <div class="code">You can also call methods with <a href="manual_api.htm#camel"><span class="var">Camel naming convertions</span></a>.<br>
          <table cellspacing="1" cellpadding="1">
            <tr>
              <th> Method </th>
              <th> Description</th>
            </tr>
            <tr>
              <td>
                <div class="returns">mixed</div>
                $e-&gt;<strong>children</strong> ( <span class="var">[int $index]</span> ) </td>
              <td>Returns the Nth <strong>child object</strong> if <strong>index</strong> is set, otherwise return an <strong>array of children</strong>. </td>
            </tr>
            <tr>
              <td>
                <div class="returns">element</div>
                $e-&gt;<strong>parent</strong> () </td>
              <td>Returns the <strong>parent</strong> of element. </td>
            </tr>
            <tr>
              <td>
                <div class="returns">element</div>
                $e-&gt;<strong>first_child</strong> () </td>
              <td>Returns the <strong>first child</strong> of element, or <strong>null</strong> if not found. </td>
            </tr>
            <tr>
              <td>
                <div class="returns">element</div>
                $e-&gt;<strong>last_child</strong> () </td>
              <td>Returns the <strong>last child</strong> of element, or <strong>null</strong> if not found. </td>
            </tr>
            <tr>
              <td>
                <div class="returns">element</div>
                $e-&gt;<strong>next_sibling</strong> () </td>
              <td>Returns the <strong>next sibling</strong> of element, or<strong> null</strong> if not found. </td>
            </tr>
            <tr>
              <td>
                <div class="returns">element</div>
                $e-&gt;<strong>prev_sibling</strong> () </td>
              <td>Returns the <strong>previous sibling</strong> of element, or <strong>null</strong> if not found. </td>
            </tr>
          </table>
      </div>
    </div>
  </div>
  <a name="section_dump"></a>
  <h2>How to dump contents of DOM object?</h2>
  <a class="top" href="#top">Top</a>
  <div id="container_dump">
    <ul>
      <li><a href="#frag_dump_quick"><span>Quick way</span></a></li>
      <li><a href="#frag_dump_oo"><span>Object-oriented way</span></a></li>
    </ul>
    <div id="frag_dump_oo">
      <div class="code"><span class="comment">// </span><span class="comment">Dumps the internal DOM tree back into string </span><br>
    $str = $html-&gt;<strong>save</strong>();<br>
    <br>
    <span class="comment">// Dumps the internal DOM tree back into a file</span> <br>
    $html-&gt;<strong>save</strong>(<span class="var">'result.htm'</span>);</div>
    </div>
    <div id="frag_dump_quick">
      <div class="code"><span class="comment">// </span><span class="comment">Dumps the internal DOM tree back into string </span><br>
    $str = $html;<br>
    <br>
    <span class="comment">// Print it!</span><br>
    echo $html; <br>
      </div>
    </div>
  </div>
  <a name="section_callback"></a>
  <h2>How to customize the parsing behavior?</h2>
  <a class="top" href="#top">Top</a>
  <div id="container_callback">
    <ul>
      <li><a href="#frag_callback"><span>Callback function</span></a></li>
    </ul>
    <div id="frag_callback">
      <div class="code"><span class="comment">// Write a function with parameter &quot;<strong>$element</strong>&quot;</span><br>
    function my_callback(<span class="var">$element</span>) {<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">// Hide all &lt;b&gt; tags </span><br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if ($element-&gt;tag==<span class="var">'b'</span>)<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$element-&gt;outertext = '';<br>
    } <br>
    <br>
    <span class="comment">// Register the callback function with it's <strong>function name</strong></span><br>
    $html-&gt;<strong>set_callback</strong>(<span class="var">'my_callback'</span>);<br>
    <br>
    <span class="comment">// Callback function will be invoked while dumping</span><br>
    echo $html; </div>
    </div>
  </div>
  <div><br>
    Author: S.C. Chen (me578022@gmail.com)<br>
    Original idea is from Jose Solorzano's <a href="http://php-html.sourceforge.net/">HTML Parser for PHP 4</a>. <br>
    Contributions by: Yousuke Kumakura (Attribute Filters) <br>
  </div>
</div>
<script class="jsbin" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.5/jquery-ui.min.js"></script>
<script language="JavaScript" type="text/JavaScript">
	$(function(){
		$('#container_create').tabs();
		$('#container_find').tabs();
		$('#container_access').tabs();
		$('#container_traverse').tabs();
		$('#container_dump').tabs();
		$("#container_callback").tabs();
	});
</script>
]]></description><pubDate>Sun, 26 Dec 2010 08:00:00 +0700</pubDate></item><item><title>แนวทาง การใช้งาน php กับ css กำหนด รูปแบบ style อย่างง่าย</title><link>http://www.ninenik.com/แนวทาง_การใช้งาน_php_กับ_css_กำหนด_รูปแบบ_style_อย่างง่าย-387.html</link><description><![CDATA[<p>กรณีไฟล์ทดสอบ สมมติให้ชื่อว่า php_css.php<br />
ในที่นี้จะแนะนำ การใช้งาน php กับ css ที่นำมาใช้งานในรูปแบบต่างๆ ได้แก่<br />
แบบ in-line<br />
แบบ Internal<br />
แบบ External</p>
<p>ศึกษาการใช้งาน css ใน 3 รูปแบบได้ที่<br />
<a href="http://www.ninenik.com/การใช้งาน_CSS__ศึกษาิวิธีการนำ_CSS_ไปใช้กับ_HTML-59.html">http://www.ninenik.com/การใช้งาน_CSS__ศึกษาิวิธีการนำ_CSS_ไปใช้กับ_HTML-59.html</a></p>
<p>1. การใช้งาน php กับ css แบบ inline โดยการแทรกค่าตัวแปร php เข้าไปใน css style แบบ inline<br />
โค้ดไฟล์ php_css.php</p>
<pre class="php:controls" name="code">
&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot;
 &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;
&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;
&lt;head&gt;
&lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=utf-8&quot; /&gt;
&lt;title&gt;php css random&lt;/title&gt;
&lt;/head&gt;

&lt;body&gt;

&lt;?php
// กำหนดค่า array สี สำหรับทดสอบ
$arr_mycolor=array(&quot;#5AB820&quot;,&quot;#E7CD3A&quot;,&quot;#3A7BE7&quot;,&quot;#E73AE1&quot;,&quot;#E73A6B&quot;);

// แบบที่ 1
// อธิบาย  array_rand($arr_mycolor) ทำการ random เอาค่า key ของ array ค่าสี
$my_randcolor=$arr_mycolor[array_rand($arr_mycolor)]; // ดึง array ค่าสีจาก key ที่ random ได้

// แบบที่ 2
//	shuffle($arr_mycolor); // ทำการสลับตำแหน่งของ array
//	$my_randcolor=$arr_mycolor[0]; // ดึงค่า array สีตัวแรกหลังสลับตำแหน่งแล้ว
?&gt;

&lt;p style=&quot;color:&lt;?=$my_randcolor?&gt;;&quot;&gt;www.ninenik.com&lt;/p&gt;

&lt;/body&gt;
&lt;/html&gt;</pre>
<p>2. การใช้งาน php กับ css แบบ Internal โดยการแทรกค่าตัวแปร php เข้าไปในส่วน head ด้วยแท็ก style<br />
โค้ดไฟล์ php_css.php</p>
<pre class="php:controls" name="code">
&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; 
&quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;
&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;
&lt;head&gt;
&lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=utf-8&quot; /&gt;
&lt;title&gt;&lt;/title&gt;
&lt;?php
// กำหนดค่า array สี สำหรับทดสอบ
$arr_mycolor=array(&quot;#5AB820&quot;,&quot;#E7CD3A&quot;,&quot;#3A7BE7&quot;,&quot;#E73AE1&quot;,&quot;#E73A6B&quot;);

// แบบที่ 1
// อธิบาย  array_rand($arr_mycolor) ทำการ random เอาค่า key ของ array ค่าสี
$my_randcolor=$arr_mycolor[array_rand($arr_mycolor)]; // ดึง array ค่าสีจาก key ที่ random ได้

// แบบที่ 2
//	shuffle($arr_mycolor); // ทำการสลับตำแหน่งของ array
//	$my_randcolor=$arr_mycolor[0]; // ดึงค่า array สีตัวแรกหลังสลับตำแหน่งแล้ว
?&gt;
&lt;style type=&quot;text/css&quot;&gt;
p{
	background-color:&lt;?=$color?&gt;;	
}
&lt;/style&gt;
&lt;/head&gt;

&lt;body&gt;

&lt;p&gt;www.ninenik.com&lt;/p&gt;

&lt;/body&gt;
&lt;/html&gt;</pre>
<p>3. การใช้งาน php กับ css แบบ External โดยการสร้างไฟล์ css จาก php ไฟล์ สมมติชื่อ gencss.php<br />
แล้วนำมาใช้ในไฟล์ php_css.php ด้วย แท็ก link<br />
โค้ตไฟล์ gencss.php</p>
<pre class="php:controls" name="code">
&lt;?php
header(&quot;Content-type:text/css; charset=UTF-8&quot;);            
header(&quot;Cache-Control: no-store, no-cache, must-revalidate&quot;);             
header(&quot;Cache-Control: post-check=0, pre-check=0&quot;, false);   
// กำหนดค่า array สี สำหรับทดสอบ
$arr_mycolor=array(&quot;#5AB820&quot;,&quot;#E7CD3A&quot;,&quot;#3A7BE7&quot;,&quot;#E73AE1&quot;,&quot;#E73A6B&quot;);

// แบบที่ 1
// อธิบาย  array_rand($arr_mycolor) ทำการ random เอาค่า key ของ array ค่าสี
$my_randcolor=$arr_mycolor[array_rand($arr_mycolor)]; // ดึง array ค่าสีจาก key ที่ random ได้

// แบบที่ 2
//	shuffle($arr_mycolor); // ทำการสลับตำแหน่งของ array
//	$my_randcolor=$arr_mycolor[0]; // ดึงค่า array สีตัวแรกหลังสลับตำแหน่งแล้ว    
?&gt;
p{
	color:&lt;?=$my_randcolor?&gt;;		
}</pre>
<p>โค้ดไฟล์ php_css.php</p>
<pre class="php:controls" name="code">
&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; 
&quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;
&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;
&lt;head&gt;
&lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=utf-8&quot; /&gt;
&lt;title&gt;&lt;/title&gt;
&lt;!--นำ css ไฟล์จากการ gen ด้วย php มาใช้งานแบบ external--&gt;
&lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;gencss.php&quot; /&gt;
&lt;/head&gt;

&lt;body&gt;

&lt;p&gt;www.ninenik.com&lt;/p&gt;

&lt;/body&gt;
&lt;/html&gt;</pre>
<p>&nbsp;</p>]]></description><pubDate>Sat, 25 Dec 2010 16:42:24 +0700</pubDate></item><item><title>สร้าง php ฟังก์ชัน ตราจสอบ ก่อนถึงวันหมดอายุ อย่างง่าย</title><link>http://www.ninenik.com/สร้าง_php_ฟังก์ชัน_ตราจสอบ_ก่อนถึงวันหมดอายุ_อย่างง่าย-384.html</link><description><![CDATA[<p>ตัวอย่าง php ฟังก์ชัน อย่างง่ายต่อไปนี้ เป็น ฟังก์ชัน สำหรับใช้ เปรียบเวลาปัจจุบัน กับวันที่จะหมออายุ<br />
ของรายการใดรายการหนึ่ง โดยอยู่ในรูปแบบเช่น</p>
<p>รายการ x จะหมดอายุในอีก 30 วัน<br />
รายการ x จะหมดอายุในอีก 15 วัน<br />
รายการ x จะหมดอายุในอีก 7 วัน<br />
.........<br />
....<br />
เป็นต้น</p>
<p>โค้ดและตัวอย่างการใช้งาน</p>
<pre class="php:controls" name="code">
&lt;?php
function status_date_notify($endDate){
	$chk_day_now=time();
	$chk_day_expire=strtotime($endDate);
	$chk_day30=strtotime($endDate.&quot; -30 day&quot;);
	$chk_day15=strtotime($endDate.&quot; -15 day&quot;);
	$chk_day7=strtotime($endDate.&quot; -7 day&quot;);
	//  สามารถเพิ่มตัวแปร และเงื่อนไข เพิ่มเติมสำหรับตรวจสอบได้ตามต้องการ
	if($chk_day_now&gt;=$chk_day_expire){
		return 5; // เงื่อนไขรายการเมื่อหมดอายุ
	}else{
		if($chk_day_now&gt;=$chk_day30 &amp;&amp; $chk_day_now&lt;$chk_day15){
			return 4; // เงื่อนไขรายการจะหมดอายุในอีก 30 วัน
		}elseif($chk_day_now&gt;=$chk_day15 &amp;&amp; $chk_day_now&lt;$chk_day7){
			return 3; // เงื่อนไขรายการจะหมดอายุในอีก 15 วัน
		}elseif($chk_day_now&gt;=$chk_day7 &amp;&amp; $chk_day_now&lt;$chk_day_expire){
			return 2; // เงื่อนไขรายการจะหมดอายุในอีก 7 วัน
		}else{
			return 1; // เงื่อนไขรายการยังไม่หมดอายุ
		}
	}
}
////////////////////////////////////////////////
//////        ตัวอย่างการประยุกต์ใช้งานอย่างง่าย
//////////////////////////////////////////////////

$case_notify=status_date_notify(&quot;2011-01-01&quot;);
switch($case_notify){
	case 5:
		echo &quot;เงื่อนไขรายการเมื่อหมดอายุ&quot;;
		break;	
	case 4:
		echo &quot;เงื่อนไขรายการจะหมดอายุในอีก 30 วัน&quot;;
		break;	
	case 3:
		echo &quot;เงื่อนไขรายการจะหมดอายุในอีก 15 วัน&quot;;
		break;	
	case 2:
		echo &quot;เงื่อนไขรายการจะหมดอายุในอีก 7 วัน&quot;;
		break;	
	default:  // กรณีค่าเท่ากับ 1  
		echo &quot;เงื่อนไขรายการยังไม่หมดอายุ&quot;;
		break;									
}
?&gt;</pre>
<p>&nbsp;</p>]]></description><pubDate>Fri, 17 Dec 2010 11:30:53 +0700</pubDate></item><item><title>แนะนำ php ip 2 country สำหรับเช็ค ip ว่ามาจากประเทศอะไร</title><link>http://www.ninenik.com/แนะนำ_php_ip_2_country_สำหรับเช็ค_ip_ว่ามาจากประเทศอะไร-383.html</link><description><![CDATA[<p>ได้ idea จากคำถามในเว็บบอร์ด เลยเอามาแนะนำ php ip 2 country<br />
สามารถเข้าไปศึกษาและดาวน์โหลด ไฟล์ได้ด้วยตัวเองที่ <br />
<a href="http://code.google.com/p/php-ip-2-country/">http://code.google.com/p/php-ip-2-country/</a></p>
<p>เป็น class php ที่เข้าเขียนไว้เรียบร้อยแล้ว สำหรับหา ว่า ip นั้นๆ ที่กำหนด มาจาก<br />
ประเทศอะไร โดยจะเป็นการใช้งานเชื่อมต่อกับฐานข้อมูล<br />
หลังจากทำการดาวน์โหลดไฟล์มาแล้ว<br />
จะมีไฟล์ อยู่ 3 ไฟล์<br />
คือ<br />
1. ไฟล์ sql (ip_to_country.sql) ข้อมูลสำหรับค่า ip range กับประเทศ ให้ import ไฟล์นี้เข้าฐานข้อมูลไว้ใช้งาน<br />
2. ไฟล์ php class (phpip2country.class.php)<br />
3. ไฟล์ index.php เป็นไฟล์ตัวอย่างการใช้งาน และข้อมูลของค่าต่างๆ ทั้งหมด</p>
<p>ตัวอย่างการเอามาประยุกต์ใช้งาน <br />
&nbsp;</p>
<pre class="php:controls" name="code">
&lt;?php
require(&quot;phpip2country/phpip2country.class.php&quot;);
        
$dbConfigArray = array(
        'host' =&gt; 'localhost', //example host name
        'port' =&gt; 3306, //3306 -default mysql port number
        'dbName' =&gt; 'test', //example db name
        'dbUserName' =&gt; 'root', //example user name
        'dbUserPassword' =&gt; 'test', //example user password
        'tableName' =&gt; 'ip_to_country', //example table name
);

// ฟังก์ชันสำหรับหา IP Address   
function getIP(){
    // ตรวจสอบ IP กรณีการใช้งาน share internet
    if(!empty($_SERVER['HTTP_CLIENT_IP'])){
      $ip=$_SERVER['HTTP_CLIENT_IP'];
    }else{
      $ip=$_SERVER['REMOTE_ADDR'];
    }
	return $ip;
}
 
   
// การเรียกใช้ IP  
$visitorIP = getIP();          
$phpIp2Country = new phpIp2Country($visitorIP,$dbConfigArray);

 //    แสดงรายละเอียด ของ ip ของผู้ใช้งาน
echo &quot;&lt;pre&gt;&quot;;	    
print_r($phpIp2Country-&gt;getInfo(IP_INFO));
echo &quot;&lt;/pre&gt;&quot;;	   
?&gt;</pre>
<p>สามารถนำรายละเอียดข้างต้น ไปประยุกต์ใช้งาน ตามต้องการ</p>
<p>เพิ่มเติม <a href="http://ip-to-country.webhosting.info/book/print/5">http://ip-to-country.webhosting.info/book/print/5</a> อาจมีประโยชน์</p>]]></description><pubDate>Tue, 14 Dec 2010 01:10:29 +0700</pubDate></item></channel></rss>
