|
 |
|
ช่วยดู Class ในการตรวจสอบลิงค์ต่างๆ ที่โพสเข้ามาในระบบหน่อยครับ |
|
 |
|
|
 |
 |
|
ผมต้องการเขียน Class สำหรับแปลงค่า Link ที่ถูกส่งเข้ามา โดยมีเงื่อนไขอยู่ว่า
1. สมมติว่าผมมีลิงค์ที่อนุญาติให้โพสอยู่ด้วยกันสัก 5 ลิงค์ เช่น http://www.pichitchai.com, http://www.hellopichitchai.com, http://www.hellopichitchai3.com, http://www.hellopichitchai4.com, http://www.hellopichitchaid.com, หากโพสลิงค์อื่นเข้ามานอกเหนือจากนี้ จะถูก Replace ไปเป็นอย่างอื่น เช่น หากโพส http://www.sanitkun.com จะถูกแปลงเป็น URL http://www.pihitchai.com ทันทีครับ (เหมือนการโพสข้อความเว็บบอร์ดครับ หากมีลิงค์ให้เช็คลิงค์ว่าเป)
ผมไม่รู้จะเขียนยังไง ยังไงรบกวนผู้รู้ช่วยดูโค๊ด และช่วยเขียนเพิ่มเติมให้ด้วยครับ มาถึงทางตันแล้วจริงๆ
Code (PHP)
<?
class ChkLink {
function Filter($input,$replace){
$input=stripslashes($input);
$regex_pattern = '/<a href="(.*)">(.*)<\/a>/';
preg_match_all($regex_pattern,$input,$matches);
$all_url = $matches[1];
$regex_pichitchai_pattern = "/http:\/\/(.*).pichitchai.com/";
foreach($all_url as $url) {
preg_match_all($regex_pichitchai_pattern,$url,$matches);
if(empty($matches[0][0])) {
$input = str_replace($url, $replace, $input);
}
}
return $input;
}
}
Tag : PHP, Class Library

|
|
 |
 |
 |
 |
Date :
2010-10-12 21:44:54 |
By :
pichitchaip |
View :
1429 |
Reply :
1 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
<?
class ChkLink {
function Filter($input,$replace){
$input=stripslashes($input);
$regex_pattern = '/<a href="(.*)">(.*)<\/a>/';
preg_match_all($regex_pattern,$input,$matches);
$all_url = $matches[1];
$regex_pichitchai_pattern = "/http:\/\/(.*).pichitchai.com/";
$pat2 = "/http:\/\/(.*).pichitchai2.com/";
$pat3 = "/http:\/\/(.*).pichitchai3.com/";
$pat4 = "/http:\/\/(.*).pichitchai4.com/";
$pat5 = "/http:\/\/(.*).pichitchai5.com/";
foreach($all_url as $url) {
preg_match_all($regex_pichitchai_pattern,$url,$matches);
preg_match_all($pat2, $url,$matches2);
preg_match_all($pat3, $url,$matches3);
preg_match_all($pat4, $url,$matches4);
preg_match_all($pat5, $url,$matches5);
if(empty($matches[0][0]) && empty($matches2[0][0]) && empty($matches3[0][0]) && empty($matches4[0][0]) && empty($matches5[0][0])) {
$input = str_replace($url, $replace, $input);
}
}
return $input;
}
}
?>
สมมุติว่าโค้ดข้างบนถูกแล้วนะครับ (ผมยังไม่ได้ check)
|
 |
 |
 |
 |
Date :
2010-10-13 04:09:04 |
By :
... |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 04
|