Bir dize parçası kaldırmak için nasıl?

6 Cevap php

Nasıl bir dize bir bölümünü Döşeme ve PHP kullanarak MySQL belirli bir dizesinde kaydetmek için?

Örnek:

Bir dize değeri varsa "REGISTER 11223344 here"

Nasıl dize "11223344" kesebilir?

6 Cevap

Özellikle "11223344" hedefleme ediyorsanız, o zaman kullanabilirsiniz str_replace :

echo str_replace("11223344", "", "REGISTER 11223344 here");

Sen olarak tanımlanan, str_replace() kullanabilir:

str_replace($search, $replace, $subject)

Yani kod olarak yazabilirsiniz:

$subject = 'REGISTER 11223344 here';
$search = '11223344'
$trimmed = str_replace($search, '', $subject);
echo $trimmed

Düzenli ifadeler yoluyla daha iyi eşleştirme gerekiyorsa kullanabilirsiniz preg_replace().

11223344 sabit değildir varsayarak

$string="REGISTER 11223344 here";
$s = explode(" ",$string);
unset($s[1]);
$s = implode(" ",$s);
print "$s\n";

Şunları yapın

$string = 'REGISTER 11223344 here';

$content = preg_replace('/REGISTER(.*)here/','',$string);

Bu "REGISTERhere dönecekti"

veya

$string = 'REGISTER 11223344 here';

$content = preg_replace('/REGISTER (.*) here/','',$string);

Bu "Burada KAYIT" dönecekti

Eğer kural tabanlı eşleştirme gerektiğinde, regex kullanmak gerekir

$string = "REGISTER 11223344 here";
preg_match("/(\d+)/", $string, $match);
$number = $match[1];

Bu sayı ilk seti maç, bu nedenle daha spesifik deneyin olması gerekiyorsa olacak:

$string = "REGISTER 11223344 here";
preg_match("/REGISTER (\d+) here/", $string, $match);
$number = $match[1];

substr() is a built-in php function which returns part of a string. The function substr() will take a string as input, the index form where you want the string to be trimmed. and an optional parameter is the length of the substring. You can see proper documentation and example code on http://php.net/manual/en/function.substr.php

NOT: Bir dize için indeks 0 ile başlar.