我有我已經寫,以便提取一個用戶輸入的值和替換一些高度和寬度值和保持URL的正則表達式。這是因爲它可以安全地添加到數據庫中。嵌入代碼的正則表達式將不起作用
這是我迄今爲止(只是想獲得的preg_match返回真值)
$test ='<object height="81" width="100%"> <param name="movie" value="http://player.soundcloud.com/player.swf?url=http%3A%2F%2Fsoundcloud.com%2Ftheshiverman%2Fsummer-beats-july-2010&secret_url=false"></param> <param name="allowscriptaccess" value="always"></param> <embed allowscriptaccess="always" height="81" src="http://player.soundcloud.com/player.swf?url=http%3A%2F%2Fsoundcloud.com%2Ftheshiverman%2Fsummer-beats-july-2010&secret_url=false" type="application/x-shockwave-flash" width="100%"></embed> </object>';
if (preg_match('/<object height=\"[0-9]*\" width=\"[0-9]*\"><param name=\"movie\" value=\"(.*)\"><\/param><param name=\"allowscriptaccess\" value=\"always\"><\/param><embed allowscriptaccess=\"always\" height=\"[0-9]*\" src=\".*\" type=\"application\/x-shockwave-flash\" width=\"100%\"><\/embed><\/object>/', $test)) {
$embed = $test;
} else {
$embed = 'FALSE';
}
我好像做錯了驗證,因爲它總是返回false。
解析(X)HTML用正則表達式是[危險的事情。(http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-自包含的標籤/ 1732454#1732454) – NullUserException 2010-08-17 06:07:58