2012-07-25 187 views

回答

5

您需要使用preg_split功能。

$str = 'Hello&World'; 
$words = preg_split('/[^\w]/',$str); 

echo $words[0]; 

您可以通過訪問$你好字[0],並通過世界$字[1]

2

您可以使用preg_match()此:

if (preg_match('/^([\w]+)/i', $string, $match)) { 
    echo "The matched word is {$match[1]}."; 
} 

變化[\w]+[a-z]+如果你不希望匹配5或數字字符。

+3

子組,'/ i'標誌和括號都可以安全地刪除。 – Ryan 2012-07-25 22:22:22

2

使用preg_split。按正則表達式拆分字符串

0

使用使preg_split獲得阿爾法的第一部分。
$ array = preg_split('/ [^ [:alpha:]] + /','Hello5World'); echo $ array [0];