本文整理汇总了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;
}