[英]How to Remove special character from a string
我想從我的字符串中刪除以下特殊字符。
:
'
""
`
``
如何從我的字符串中刪除上述每個字符?
使用 str_replace:
$to_remove = array(':', "'", '"', '`'); // Add all the characters you want to remove here
$result = str_replace($to_remove, '', $your_string);
這將用空字符串替換 $to_remove 數組中的所有字符,實質上是刪除它們。
你可以使用preg_replace
$string = preg_replace('/[:'" `]/', '', $string);
這是比使用正則表達式的 preg_replace 更有效的解決方案:
$string = str_replace(array(':',"'",'"','`'), '', $sourceString);
您可以在 php 文檔中閱讀有關 str_replace 和 preg_replace 的更多信息:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.