2016-01-05 28 views
0

早上好,用特定分隔符截斷PHP字符串

我需要用特定的分隔符截斷字符串。 例如以該字符串:

myString = 'customname_489494984'; 

我想每個字符串份後自動剝離「_」 (在這種情況下「489494984」)。

是否有一個函數用於在特定的 定界符之後截斷字符串部分?

非常感謝!

弗朗索瓦

回答

1

使用substrstrpos一個簡單的組合:

$myString = 'customname_489494984'; 
echo substr($myString, 0, strpos($myString, '_')); 

獎金 -包裝在一個自定義函數:

function truncateStringAfter($string, $delim) 
{ 
    return substr($string, 0, strpos($string, $delim)); 
} 
echo truncateStringAfter('customname_489494984', '_'); 
0

試試這個,它會刪除任何東西在「_」之後。

$string= 'customname_489494984'; 
$string=substr($string, 0, strrpos($string, '_')); 
+0

AFER ** - ** **或** _ ? :) –

+0

@阿里,後'_'。我編輯了我的答案 – user3040610

+1

好吧。我只是說。我知道這是一個輸入錯誤。 –

0

其他直截了當的方法的問題是,如果字符串不包含「_」,那麼將返回一個空字符串。這是一個小型的StringUtils類(一個較大的類的一部分),它以多字節安全方式通過beforeFirst靜態方法實現。

var_dump(StringUtils::beforeFirst("customname_489494984", "_")); 
class StringUtils 
{ 
    /** 
    * Returns the part of a string <b>before the first</b> occurrence of the string to search for. 
    * @param <b>$string</b> The string to be searched 
    * @param <b>$search</b> The string to search for 
    * @param <b>$caseSensitive boolean :optional</b> Defines if the search will be case sensitive. By default true. 
    * @return string 
    */ 
    public static function beforeFirst($string,$search,$caseSensitive = true) 
    { 
    $firstIndex = self::firstIndexOf($string, $search,$caseSensitive); 
    return $firstIndex == 0 ? $string : self::substring($string, 0 , $firstIndex); 
    } 


    /** 
    * Returns a part of the string from a character and for as many characters as provided 
    * @param <b>$string</b> The string to retrieve the part from 
    * @param <b>$start</b> The index of the first character (0 for the first one) 
    * @param <b>$length</b> The length of the part the will be extracted from the string 
    * @return string 
    */ 
    public static function substring($string,$start,$length = null) 
    { 
    return ($length == null) ? (mb_substr($string, $start)) : ($length == 0 ? "" : mb_substr($string, $start , $length)); 
    } 

    /** 
    * Return the index of <b>the first occurance</b> of a part of a string to the string 
    * @param <b>$string</b> The string to be searched 
    * @param <b>$search</b> The string to search for 
    * @param <b>$caseSensitive boolean :optional</b> Defines if the search will be case sensitive. By default true. 
    * @return number 
    */ 
    public static function firstIndexOf($string,$search,$caseSensitive = true) 
    { 
    return $caseSensitive ? mb_strpos($string, $search) : mb_stripos($string, $search); 
    } 

    /** 
    * Returns how many characters the string is 
    * @param <b>$string</b> The string 
    * @return number 
    */ 
    public static function length($string) 
    { 
    return mb_strlen($string); 
    } 

} 
3

您還可以使用的strstr(),它發現一個字符串的第一次出現:

$myString= "customname_489494984"; 
echo strstr($myString, '_', true); 

這裏是我的參考:http://php.net/manual/en/function.strstr.php

+0

這是最好的答案。添加一個鏈接到PHP文檔的strstr(),它是完美的。 – mopo922

+0

@ mopo922,我編輯了我的答案並添加了我的參考鏈接。謝謝! –