簡體   English   中英

在php中使用正則表達式查找結果str

[英]Find result str with regular expressions in php

我有一個輸入字符串:

$str1 = "some usefull text and garbage `~#@!&^*(()}{./";
$str2 = "`~#@!&^*(()}{./";

$result = Exclude with regular expressions all symbols from str1, which are in str2.
$result = "some usefull text and garbage";

什么正則表達式將刪除我指定的所有符號? 如何以正確的方式過濾它? 感謝名單!

您不需要正則表達式:

$clean_str = str_replace(str_split($str2), '', $str1);

您可能希望trim結果字符串。

以下是使用正則表達式的示例:

$str1 = "some usefull text and garbage `~#@!&^*(()}{./";
$str2 = "`~#@!&^*(()}{./";

$pattern = preg_quote($str2,'/'); 
echo preg_replace('/'.$pattern.'/', "", $str1);

輸出:

some usefull text and garbage 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM