我想在HTML內容中進行字符串替換。使用字符串獲取值的正則表達式
<!-- REPLACE_STRING_5 -->
爲了做到這一點,我需要得到的數出字符串(ID),我只是想檢查我是正確,高效地這樣做呢?
<?php
$subject = "<!-- REPLACE_STRING_21 -->";
$pattern = '/^<!-- REPLACE_STRING_[0-9\.\-]+ -->/';
if(preg_match($pattern, $subject))
{
$pos = strpos($subject, '-->');
//20 is the number where the number postion start
$pos = $pos - 20;
echo substr($subject, 20, $pos);
}
else
{
echo 'not match';
}
當您完成後,您希望REPLACE_STRING_21看起來像什麼? – brandonscript
我需要將'21'(21是ID)取出並查找數據庫,然後獲取內容並替換此'<! - REPLACE_STRING_21 - >' – Bruce