2009-06-30 4 views
9

"또는"뒤에 문자열을 나눌 좋은 방법이 있습니다. ?PHP로 문자열을 분할하는 방법

처럼

$string = "test.test" result = test 

$string = "test doe" result = test 

내가 두 번 폭발 사용할 수 있지만, 나는 확실히 그게 전부하지 최상의 솔루션을 나는 물론)

답변

15

여러 다른 문자에 분할 할 경우, preg_split를 살펴

//split string on space or period: 
$split=preg_split('/[ \.]/', $string); 
+0

eek, well spotted –

+0

잘 작동합니다. – opHASnoNAME

1

strtr을 수행 할 수 있습니다. 우주로 폭발 한 다음 우주로 폭발합니다. 이후 strtr 매우 빠릅니다.

3

strtok 기능을 원하십니까? 귀하의 경우

<?php 
$string = "This is\tan example\nstring"; 
/* Use tab and newline as tokenizing characters as well */ 
$tok = strtok($string, " \n\t"); 

while ($tok !== false) { 
    echo "Word=$tok<br />"; 
    $tok = strtok(" \n\t"); 
} 
?> 

비록 내가 explode를 사용하여 두 번 더 쉽게 더 잘 보이는 의심 : 설명서는이 예제를 제공합니다.

관련 문제