2016-12-05 7 views
0

Wie reduziere ich meine str_replace PHP-Code?Wie reduziere ich meinen str_replace PHP-Code?

Ich möchte AASS bis Zaß von

dies ist mein PHP-Code entfernen, ist es Arbeit gut. Aber ich möchte reduzieren. wie kann ich ?

<?PHP 
$str = str_replace('aass', '', $str); 
$str = str_replace('bass', '', $str); 
$str = str_replace('cass', '', $str); 
$str = str_replace('dass', '', $str); 
$str = str_replace('eass', '', $str); 
$str = str_replace('fass', '', $str); 
$str = str_replace('gass', '', $str); 
$str = str_replace('hass', '', $str); 
$str = str_replace('iass', '', $str); 
$str = str_replace('jass', '', $str); 
$str = str_replace('kass', '', $str); 
$str = str_replace('lass', '', $str); 
$str = str_replace('mass', '', $str); 
$str = str_replace('nass', '', $str); 
$str = str_replace('oass', '', $str); 
$str = str_replace('pass', '', $str); 
$str = str_replace('qass', '', $str); 
$str = str_replace('rass', '', $str); 
$str = str_replace('sass', '', $str); 
$str = str_replace('tass', '', $str); 
$str = str_replace('uass', '', $str); 
$str = str_replace('vass', '', $str); 
$str = str_replace('wass', '', $str); 
$str = str_replace('xass', '', $str); 
$str = str_replace('yass', '', $str); 
$str = str_replace('zass', '', $str); 
?> 
+0

Siehe auch: http://stackoverflow.com/q/8163746 Sie einfach Arrays verwenden können, für Ihre Argumente. Sie könnten auch eine Regex mit 'preg_replace()' verwenden, wenn Sie möchten. – Rizier123

Antwort

0

Sie können ein Array verwenden, um zu suchen.

http://php.net/manual/en/function.str-replace.php

$search = array("aass", "bass", "cass", "dass", "eass", "fass", "gass", "hass", "iass", "jass", "kass", "lass", "mass", "nass", "oass", "pass", "qass", "rass", "sass", "tass", "uass", "vass", "wass", "xass", "yass", "zass"); 
$str = str_replace($search, "", $str); 

Alternativ können Sie Wiktor Stribiżew Antwort sehen, wie, dass man noch weiter vereinfacht ist.

0

Verwendung mit Array. Wie unten Code:

$arrayKey = array('aass', 'bass', 'cass'.....); 
$arrayReplace = array('','',''); 
$str = str_replace($arrayKey, $arrayReplace, $str); 

ODER

$arrayKey = array('aass', 'bass', 'cass'.....); 
    $arrayReplace = ""; 
    $str = str_replace($arrayKey, $arrayReplace, $str); 
1
<?php 
    $pattern = '/[a-z]ass/'; 
    $replacement = ''; 
    echo preg_replace($pattern, $replacement, $str); 
?> 
7

Verwenden Sie einen regulären Ausdruck:

$res = preg_replace('~[a-z]ass~', '', $str); 

Die [a-z] Zeichenklasse alle Klein ASCII Buchstaben.

Siehe die regex demo.

0

Verwenden preg_replace

Beispiel:

echo preg_replace('/([s\S]*ass)/', '', $str); 

[s\S]* enthält alles, Zahlen, Buchstaben usw.