2016-12-17 22 views
1

如何使用preg_replace php刪除字符串中的font-family樣式?如何使用preg_replace php刪除字符串中的font-family樣式?

我試過使用這段代碼,但沒有工作。我怎樣才能做到這一點 ?

<?PHP 
if(isset($_POST["submit"])) 
{ 
    $editor = $_POST['editor']; 
    $editor = preg_replace("#font-family(.*?)>(.*?);#is", "", $editor); 
    echo $editor; 
} 
?> 


<form class="form" method="post" action="" ENCTYPE = "multipart/form-data"> 
<textarea name="editor" style="margin: 0px;width: 415px;height: 30px;" ><p style="font-family: fantasy; font-size: 34px;">test</p></textarea> 
<br> 
<input type="submit" name="submit" value="OK"> 
</form> 
+0

什麼是「#」應該做的? – TigOldBitties

+0

@TigOldBitties'preg_replace'需要圍繞正則表達式的分隔符。它可以是任何角色,他使用'#'。 – Barmar

+0

@Barmar即使它仍然是錯誤的。我從未見過它。最後的「是」是什麼?正如我所說,在他提供的背景下,對我來說這沒有意義。 – TigOldBitties

回答

0
font-family.+?; 

會匹配到 'FONT-FAMILY:幻想;'在你的情況。你可以用空字符串替換它。你的正則表達式對我來說沒有任何意義。

也許這將幫助您瞭解什麼是錯的比我能解釋一下https://regex101.com/

你看上去有一些麻煩了解正則表達式。 LE:你的代碼應該是。

if(isset($_POST["submit"])) 
{ 
    $editor = $_POST['editor']; 
    $editor = preg_replace('/font-family.+?;/', "", $editor);; 
    echo $editor; 
} 
+0

警告:preg_replace():分隔符不能是字母數字或反斜槓 –

+0

$ editor = preg_replace(「font-family。+ ?;」,「」,$ editor); –

+0

你在開玩笑吧? preg_replace('/ font-family。+?; /',「」,$ editor);閱讀文檔的人。 – TigOldBitties

相關問題