จัดรูปแบบ url ลิ้งค์ link ด้วย เทคนิค php ได้อย่างง่าย
03 September 2010การใช้ฟังก์ชัน ที่จะแนะนำต่อไปนี้ สามารถนำไปประยุกต์ใช้กับการจัดรูปแบบ url ของ ลิ้งค์ link ให้กับข้อมูล
ที่ทำการดึงจากฐานข้อมูล เฃ่น ลิ้งค์หรือ url ที่โพสในเว็บบอร์ด บทความ ข่าว หรืออื่นๆ หากมีไอเดีย ที่หลากหลาย สามารถปรับเพิ่มเติม ให้ใช้งานอย่างมีประสิทธิภาพได้
ตัวอย่าง
http://www.ninenik.com/demo/link_replace_full.php
โค้ตตัวอย่างด้านล่าง เป็นตัวอย่างการใช้งาน เพื่อประยุกต์ กับการปรับ url แทนการใช้ jQuery จากบทความ
<?php
$dataHTML=<<<BOF
<div class="iTopicQUE_R">
<p>แนะนำ 2 เว็บด้านล่าง ทำ tag cloud จากฐานข้อมูล จะเป็นเว็บที่ 2<br />
<a href="http://www.thaicreate.com/community/tag-cloud.html" >
http://www.thaicreate.com/community/tag-cloud.html</a><br />
<a href = "http://www.narongrit.net/download-readdownload-id63.html" >
http://www.narongrit.net/download-readdownload-id63.html</a></p>
<a href= "www.google.com">Google</a><br />
<a href='code.google.com' >Code Goodle</a><br />
<a Href="#">anchor</a><br />
<a href="mailto:ninenik@gmail.com">mail to</a><br />
<a href="?data=3">Some Argument</a><br />
<a href=index.html?data=3>Some Argument 2</a><br />
<a href="www.ninenik.com">Ninenik.com</a><br />
</div>
BOF;
echo "<strong>ตัวอย่าง ส่วนของเนื้อหาที่ยังไม่ได้จัดรูปแบบ ของ ลิ้งค์</strong><br/>";
echo $dataHTML;
echo "<hr>";
echo "<strong>ตัวอย่าง ส่วนของเนื้อหาที่ จัดรูปแบบ ของ ลิ้งค์ แล้ว</strong><br/>";
function adjustLink($matches){
$linkMody="http://www.ninenik.com/weblink/?"; // รูปแบบลิ้งค์ที่นำไปปรับเพิ่ม
$siteDomain="www.ninenik.com"; // domain เว็บที่ไม่ต้องกำหนดรูปแบบ ลิ้งค์ใหม่
$matchesData=strtolower($matches[0]);
$dom = new DOMDocument;
libxml_use_internal_errors(true);
$dom->loadHTML($matchesData."</a>");
libxml_use_internal_errors(false);
$xpath = new DOMXPath($dom);
$LinkTag = $xpath->query('//a[@href]');
foreach ($LinkTag as $val) {
$matchesData=trim($val->getAttribute('href'));
}
if(preg_match("/^(mailto:)|^(#)|^(\?)/",$matchesData)){
return "<a href=\"$matchesData\">";
}
if(!preg_match("@^(https?://)@",$matchesData)){
if(preg_match("/$siteDomain/i",$matchesData)){
return "<a href=\"http://".$matchesData."\">";
}else{
return "<a href=\"".$linkMody."http://".$matchesData."\">";
}
}else{
if(preg_match("/$siteDomain/i",$matchesData)){
return "<a href=\"$matchesData\">";
}else{
return "<a href=\"".$linkMody.$matchesData."\">";
}
}
}
?>
การใช้งาน
<?php // การใช้งาน // $dataHTML คือ ข้อมูล html สามารถส่งค่าตัวแปรมาได้ // adjustLink คือชื่อฟังก์ชัน ที่ใช้สำหรับจัดรูปแบบของ url ลิ้งค์ link $pathen="/<a[^<]+?>/"; echo preg_replace_callback($pathen,"adjustLink",$dataHTML); ?>
บทความคนเข้าอ่านวันนี้
25 Sep 08 ฟังก์ชั่นเกี่ยวกับ วันที่ เวลา ใน javascript อ่าน 7135 08 Oct 10 การอัพเดท สถานะบน facebook อัตโนมัติ แบบ graph api ด้วย php sdk อ่าน 3534 09 Jul 10 เพิ่มความเร็ว ให้กับการ cache ด้วย jquery ajax และ php cache class อ่าน 2299 05 Nov 08 CSS สร้างเมนูแนวนอน 2 ชั้นโดยไม่ใช้ javascript และตาราง table อ่าน 10257 17 Sep 10 กำหนด infowindow ให้กับตัว marker จำนวนมาก ใน google map อ่าน 2932 25 Sep 08 ตรวจสอบระดับความปลอดภัยของ รหัสผ่านด้วย Ajax อ่าน 3928 06 Mar 11 แนะนำ ปลั๊กอิน comment ตัวใหม่ของ facebook อ่าน 3274
