Bunu yapmak için, benim nasıl cümle bu dizideki kelimelerden birini eklerseniz şey yapmak istiyorum?
$sentence = "I dont give a badwordtwo";
$values = array("badwordone","badwordtwo","badwordthree","badwordfour");
Teşekkürler ...
Bazı dize kelimelerin bir dizi sansür isterseniz kullanabilirsiniz str_ireplace
a> yapabilirsiniz:
$var = "This is my phrase.";
$var = str_ireplace( array("this", "phrase"), array("****", "*****"), $var);
edit: chacha102 notları gibi, sadece yıldızlı sayısını değiştirmek için ikinci dizi kullanmak gerekir,
$var = str_ireplace( array("this", "phrase"), "", $var);
aynı derecede geçerlidir. Ben de ikinci bir dizi kullanırsanız, bu uzunluk tam olarak ilk dizi eşleşmelidir olduğunu dikkat etmelisiniz, ve yedek endeksi ile gelmektedir.
Ben bir süre önce benzer bir soru vardı. Bu cevap mükemmel bir uyum olmalıdır.
http://stackoverflow.com/questions/1452622/is-this-efficient-coding-for-anti-spam/1452641#1452641
<?PHP
$banned = array('bad','words','like','these');
$looksLikeSpam = false;
foreach($banned as $naughty){
if (strpos($string,$naugty) !== false){
$looksLikeSpam=true;
}
}
if ($looksLikeSpam){
echo "You're GROSS! Just... ew!";
die();
}
?>
tek yön
$sentence = "I dont give a badwordtwo";
$values = array("badwordone","badwordtwo","badwordthree","badwordfour");
$s = explode(" ",$sentence);
foreach ($s as $a=>$b){
if (in_array($b, $values)) {
echo "Got $b";
}
}
çıktı
$ php test.php
Got badwordtwo
VEYA
$sentence = "I dont give a badwordtwo";
$values = array("badwordone","badwordtwo","badwordthree","badwordfour");
$s = explode(" ",$sentence);
var_dump(array_intersect($s, $values));
çıktı
$ php test.php
array(1) {
[4]=>
string(10) "badwordtwo"
}
Sadece aşk yok php.net?
Örnek 1 Temel str_replace () örnekleri
<?php
// Provides: <body text='black'>
$bodytag = str_replace("%body%", "black", "<body text='%body%'>");
// Provides: Hll Wrld f PHP
$vowels = array("a", "e", "i", "o", "u", "A", "E", "I", "O", "U");
$onlyconsonants = str_replace($vowels, "", "Hello World of PHP");
// Provides: You should eat pizza, beer, and ice cream every day
$phrase = "You should eat fruits, vegetables, and fiber every day.";
$healthy = array("fruits", "vegetables", "fiber");
$yummy = array("pizza", "beer", "ice cream");
$newphrase = str_replace($healthy, $yummy, $phrase);
// Provides: 2
$str = str_replace("ll", "", "good golly miss molly!", $count);
echo $count;
?>
Bu Kohana 3 bir parçası olduğunu. Hep yararlı bir fonksiyon olarak buldum. Ayrıca kısmi kelime (ya da değil) sansür sağlar.
public static function censor($str, $badwords, $replacement = '#', $replace_partial_words = TRUE)
{
foreach ((array) $badwords as $key => $badword)
{
$badwords[$key] = str_replace('\*', '\S*?', preg_quote((string) $badword));
}
$regex = '('.implode('|', $badwords).')';
if ($replace_partial_words === FALSE)
{
$regex = '(?<=\b|\s|^)'.$regex.'(?=\b|\s|$)';
}
$regex = '!'.$regex.'!ui';
if (strlen($replacement) == 1)
{
$regex .= 'e';
return preg_replace($regex, 'str_repeat($replacement, strlen(\'$1\'))', $str);
}
return preg_replace($regex, $replacement, $str);
}