本文整理匯總了PHP中string::count方法的典型用法代碼示例。如果您正苦於以下問題:PHP string::count方法的具體用法?PHP string::count怎麽用?PHP string::count使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類string
的用法示例。
在下文中一共展示了string::count方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。
示例1: param_count
/**
* @en Count total query params in the url
* @ru Возвращает количество параметров в ссылке
*
* @param string $url
*
* @return int
*/
public static function param_count($url)
{
return string::count($url, '?') + string::count($url, '&');
}
示例2: lines
/**
* @en Return specific lines from the string (or all lines if range is not provided)
* @ru Возвращает определённые строки из строки (или все строки в виде массива, если диапазон не был указан при вызове)
*
* $string = "First line\nSecond line\nThird line\netc";
*
* $lines = string::lines($string); # Array
* # (
* # [0] => First line
* # [1] => Second line
* # [2] => Third line
* # [3] => etc
* # )
*
* $lines = string::lines($string, 2); # Array
* # (
* # [0] => Second line
* # [1] => Third line
* # [2] => etc
* # )
*
* $lines = string::lines($string, 2, 3); # Array
* # (
* # [0] => Second line
* # [1] => Third line
* # )
*
*
* @param string $string Any string
* @param int $from
* @param int $to
* @param string $delimiter Line delimiter
*
* @return array
*/
public static function lines($string, $from = null, $to = null, $delimiter = PHP_EOL)
{
if ($from === null) {
return explode($delimiter, $string);
}
if ($to === null) {
$to = string::count($string, $delimiter) + 1;
}
if ($from < 1 or $from > $to) {
return null;
}
$lines = explode($delimiter, $string);
if (count($lines) < $to) {
return null;
}
$result = null;
for ($i = $from - 1; $i <= $to - 1; $i++) {
$result[] = $lines[$i];
}
return $result;
}