2011-11-26 80 views
0

我已經做出了描述系統,但所有這些都無法正常工作是我的「搜索並刪除不需要的標籤」 - 系統...下面的代碼:如何搜索HTML標記,並刪除它

$mystring = $description; 
$findme = '<'; 
$pos = strpos($mystring, $findme); 
echo $pos; 

if ($pos == false) { 
    $user = mysql_query("SELECT * FROM members WHERE username='$myusername' AND password='$mypassword'"); 
    while($row=mysql_fetch_array($user)) { 
     mysql_query("UPDATE members SET description = '$description' WHERE username = '$myusername' AND password = '$mypassword'"); 
     echo "<tr><td colspan='2'><span style='color:#0076c9;'>Your description has successfully been changed! Go back to your page to see the changes!</span></td></tr>"; 
    } 
} else { 
    echo "<tr><td colspan='2'><span style='color:#F00;'>Invalid input! Make sure that no other code than <br /> can be typed in!</span></td></tr>"; 
} 

有沒有任何可能的方式做到這一點?我在YouTube上看過它,但太棒了!任何人知道如何做到這一點?

哦,我也注意到,我的功能只顯示第一個標籤的位置...並非所有的標籤,它不能看到<br />(允許)和<img>(不允許)之間的區別。

回答

0

你必須要在更多的標籤添加或只是讓一般通過取出部分括號:

if(preg_match("/<(span|html|table).*?>/", $description)) { 
    rejected... 
} 
+0

我想我可能已經圍繞minunderstood首次文本。如果你正在尋找的不是驗證,而是去掉標籤,那麼@Kleist給出的答案可能是最好的。 – Godwin

1

使用內置strip_tags - 功能。

串用strip_tags(字符串$海峽[,字符串$ allowable_tags])

例子:

$str = "<a href='http://google.com'>Here</a> we go<br />"; 
echo strip_tags($str, "<br>"); 

輸出: 「在這裏,我們去< BR/>」