Ben bu var:
15_some_text_or_numbers;
I want to get whats in front of the first underscore. There is always a letter directly after the first underscore.
Örnek:
14_hello_world = 14
Sonuç sayısı 14!
Teşekkürler
Önünde always bir numara varsa, kullanabilirsiniz
echo (int) '14_hello_world';
String conversion to integers in the PHP manual üzerindeki girişine bakın
İşte tiplemeleri olmadan bir versiyonu:
$str = '14_hello_1world_12';
echo substr($str, 0, strpos($str, '_'));
Hiçbir çizgi bulunursa, bu, hiçbir şey dönecektir unutmayın. Eğer bulunursa typecasted sonuç (çok önemli olacağını değil) bir tamsayı olacak ise, dönüş değeri, bir dize olacaktır. Bunun yerine hiçbir çizgi var olduğunda tüm dize iade edilmesini istemelerini istiyorsanız, kullanabileceğiniz
$str = '14_hello_1world_12';
echo str_replace(strstr($str, '_'), '', $str);
PHP5.3 itibariyle de strstr
$before_needle
ayarlamak true ile kullanabileceğiniz
echo strstr('14_hello_1world_12', '_', true);
Note: As typecasting from string to integer in PHP follows a well defined and predictable behavior and this behavior follows the rules of Unix' own strtod
karışık dizeleri için, ben ilk yaklaşım isleminden kötüye nasıl görmüyorum. Em>