2013-08-02 102 views
1
function regexp($text){ 
    $text = preg_replace('#!test=#', '', $text); 
    $text = preg_replace('#@test=#', '', $text); 
    return $text; 
} 

如何合併此正則表達式?他們只有不同!和@。在我的例子中是錯誤,並且這不起作用。我想簡單地說:合併兩個正則表達式

$a = 'asdf!test=wer'; 
$b = '[email protected]=dsf'; 

變化:

$a = 'asdfwer'; 
$b = '232dsf'; 

用正則表達式。

+2

'的preg_replace( '#[!@]測試=#', '',$文本)'? – DCoder

回答

4

喜歡的東西所以應該符合兩個:#(!|@)test=#。如果你將有更多的跡象(比!@等,像這樣的東西取代它:#[[email protected]]test=#(由@DCoder的建議),將產生清潔的正則表達式

基本上在正則表達式語言管道(|)表示。 OR。所以基本上,我是說,如果它是由!@符號前綴匹配字符串test=

+0

好的回答比我...你應得的 – Gautam3164

+0

@ Gautam3164:謝謝。 – npinti

0

嘗試preg_replace用正則表達式的陣列取代了數組一樣

$text = preg_replace(array('#!test=#' , '#@test=#'),array('' , ''), $text);