无需言 做自己 业 ,精于勤 荒于嬉.

字符串 函数 strncasecmp 二进制安全比较字符串开头的若干个字符(不区分大小写)

发表日期:2021-07-01 10:23:24 | 来源: | 分类:字符串 函数

strncasecmp

(PHP 4 >= 4.0.2, PHP 5, PHP 7, PHP 8)

strncasecmp二进制安全比较字符串开头的若干个字符(不区分大小写)

说明

strncasecmp(string $str1, string $str2, int $len): int

该函数与 strcasecmp() 类似,不同之处在于你可以指定两个字符串比较时使用的长度(即最大比较长度)。

参数

str1

第一个字符串。

str2

第二个字符串。

len

最大比较长度。

返回值

如果 str1 小于 str2 返回 < 0; 如果 str1 大于 str2 返回 > 0;如果两者相等,返回 0。

参见

  • strncmp() - 二进制安全比较字符串开头的若干个字符
  • preg_match() - 执行匹配正则表达式
  • substr_compare() - 二进制安全比较字符串(从偏移位置比较指定长度)
  • strcasecmp() - 二进制安全比较字符串(不区分大小写)
  • stristr() - strstr 函数的忽略大小写版本
  • substr() - 返回字符串的子串

阅读全文 »

字符串 函数 strncmp 二进制安全比较字符串开头的若干个字符

发表日期:2021-07-01 10:23:24 | 来源: | 分类:字符串 函数

strncmp

(PHP 4, PHP 5, PHP 7, PHP 8)

strncmp二进制安全比较字符串开头的若干个字符

说明

strncmp(string $str1, string $str2, int $len): int

该函数与 strcmp() 类似,不同之处在于你可以指定两个字符串比较时使用的长度(即最大比较长度)。

注意该比较区分大小写。

参数

str1

第一个字符串。

str2

第二个字符串。

len

最大比较长度。

返回值

如果 str1 小于 str2 返回 < 0; 如果 str1 大于 str2 返回 > 0;如果两者相等,返回 0。

参见

  • strncasecmp() - 二进制安全比较字符串开头的若干个字符(不区分大小写)
  • preg_match() - 执行匹配正则表达式
  • substr_compare() - 二进制安全比较字符串(从偏移位置比较指定长度)
  • strcmp() - 二进制安全字符串比较
  • strstr() - 查找字符串的首次出现
  • substr() - 返回字符串的子串

阅读全文 »

字符串 函数 strrchr 查找指定字符在字符串中的最后一次出现

发表日期:2021-07-01 10:23:24 | 来源: | 分类:字符串 函数

      示例1
<?php 
// 获取 $PATH 中不含磁盘符号的目录$dir = substr(strrchr($PATH, ":"), 1);
// 获取最后一行内容$text = "Line 1\nLine 2\nLine 3";
$last = substr(strrchr($text, 10), 1 );
?>

阅读全文 »

字符串 函数 similar_text 计算两个字符串的相似度

发表日期:2021-07-01 10:23:23 | 来源: | 分类:字符串 函数

similar_text

(PHP 4, PHP 5, PHP 7, PHP 8)

similar_text计算两个字符串的相似度

说明

similar_text(string $first, string $second, float &$percent = ?): int

两个字符串的相似程度计算依据 Programming Classics: Implementing the World's Best Algorithms by Oliver (ISBN 0-131-00413-1) 的描述进行。注意该实现没有使用 Oliver 虚拟码中的堆栈,但是却进行了递归调用,这个做法可能会导致整个过程变慢或变快。也请注意,该算法的复杂度是 O(N**3),N 是最长字符串的长度。

参数

first

第一个字符串。

second

第二个字符串。

percent

通过引用方式传递第三个参数,similar_text() 将计算相似程度百分数。

返回值

返回在两个字符串中匹配字符的数目。

参见

  • levenshtein() - 计算两个字符串之间的编辑距离
  • soundex() - Calculate the soundex key of a string

阅读全文 »

字符串 函数 sprintf 返回格式化字符串

发表日期:2021-07-01 10:23:23 | 来源: | 分类:字符串 函数

      示例1
<?php 
$num = 5;
$location = 'tree';
$format = 'There are %d monkeys in the %s';
echo sprintf($format, $num, $location);
?>

      示例2
<?php 
$format = 'The %s contains %d monkeys';
echo sprintf($format, $num, $location);
?>

      示例3
<?php 
$format = 'The %2$s contains %1$d monkeys';
echo sprintf($format, $num, $location);
?>

      示例4
<?php 
$format = 'The %2$s contains %1$d monkeys.           That\'s a nice %2$s full of %1$d monkeys.';
echo sprintf($format, $num, $location);
?>

      示例5
<?php 
echo sprintf("%'.9d\n", 123);
echo sprintf("%'.09d\n", 123);
?>

      示例6
<?php 
$format = 'The %2$s contains %1$04d monkeys';
echo sprintf($format, $num, $location);
?>

      示例7
<?php 
$isodate = sprintf("%04d-%02d-%02d", $year, $month, $day);
?>

      示例8
<?php 
$money1 = 68.75;
$money2 = 54.35;
$money = $money1 + $money2;
echo $money;
echo "\n";
$formatted = sprintf("%01.2f", $money);
echo $formatted;
?>

      示例9
<?php 
$number = 362525200;
echo sprintf("%.3e", $number);
?>

阅读全文 »

字符串 函数 parse_str 将字符串解析成多个变量

发表日期:2021-07-01 10:23:23 | 来源: | 分类:字符串 函数

      示例1
<?php 
$str = "first=value&arr[]=foo+bar&arr[]=baz";
// 推荐用法parse_str($str, $output);
echo $output['first'];
  // valueecho $output['arr'][0];
 // foo barecho $output['arr'][1];
 // baz// 不建议这么用parse_str($str);
echo $first;
  // valueecho $arr[0];
 // foo barecho $arr[1];
 // baz?>

      示例2
<?php 
parse_str("My Value=Something");
echo $My_Value;
 // Somethingparse_str("My Value=Something", $output);
echo $output['My_Value'];
 // Something?>

阅读全文 »

字符串 函数 soundex Calculate the soundex key of a string

发表日期:2021-07-01 10:23:23 | 来源: | 分类:字符串 函数

      示例1
<?php 
soundex("Euler")       == soundex("Ellery");
    // E460soundex("Gauss")       == soundex("Ghosh");
     // G200soundex("Hilbert")     == soundex("Heilbronn");
 // H416soundex("Knuth")       == soundex("Kant");
      // K530soundex("Lloyd")       == soundex("Ladd");
      // L300soundex("Lukasiewicz") == soundex("Lissajous");
 // L222?>

阅读全文 »

字符串 函数 sscanf 根据指定格式解析输入的字符

发表日期:2021-07-01 10:23:23 | 来源: | 分类:字符串 函数

      示例1
<?php 
// getting the serial numberlist($serial) = sscanf("SN/2350001", "SN/%d");
// and the date of manufacturing$mandate = "January 01 2000";
list($month, $day, $year) = sscanf($mandate, "%s %d %d");
echo "Item $serial was manufactured on: $year-" . substr($month, 0, 3) . "-$day\n";
?>

      示例2
<?php 
// get author info and generate DocBook entry$auth = "24\tLewis Carroll";
$n = sscanf($auth, "%d\t%s %s", $id, $first, $last);
echo "<author id='$id'>    <firstname>$first</firstname>    <surname>$last</surname></author>\n";
?>

阅读全文 »

字符串 函数 str_ends_with Checks if a string ends with a given substring

发表日期:2021-07-01 10:23:23 | 来源: | 分类:字符串 函数

      示例1
<?php 
if (str_ends_with('abc', '')) {
    echo "All strings end with the empty string";
}
?>

      示例2
<?php 
$string = 'The lazy fox jumped over the fence';
if (str_ends_with($string, 'fence')) {
    echo "The string ends with 'fence'\n";
}
if (str_ends_with($string, 'Fence')) {
    echo 'The string ends with "fence"';
}
 else {
    echo '"fence" was not found because the case does not match';
}
?>

阅读全文 »

字符串 函数 str_getcsv 解析 CSV 字符串为一个数组

发表日期:2021-07-01 10:23:23 | 来源: | 分类:字符串 函数

str_getcsv

(PHP 5 >= 5.3.0, PHP 7, PHP 8)

str_getcsv 解析 CSV 字符串为一个数组

说明

str_getcsv(
    string $input,
    string $delimiter = ",",
    string $enclosure = '"',
    string $escape = "\\"
): array

CSV 字段格式解析字符串输入,并返回包含读取字段的数组。

参数

input

待解析的字符串。

delimiter

设定字段界定符(仅单个字符)。

enclosure

设定字段包裹字符(仅单个字符)。

escape

设置转义字符(仅单个字符)。默认为反斜线(\)。

返回值

返回一个包含读取到的字段的索引数组。

参见

  • fgetcsv() - 从文件指针中读入一行并解析 CSV 字段

阅读全文 »

字符串 函数 sha1 计算字符串的 sha1 散列值

发表日期:2021-07-01 10:23:23 | 来源: | 分类:字符串 函数

      示例1
<?php 
$str = 'apple';
if (sha1($str) === 'd0be2dc421be4fcd0172e5afceea3970e2f3d940') {
    echo "Would you like a green or red apple?";
}
?>

阅读全文 »

字符串 函数 str_contains Determine if a string contains a given substring

发表日期:2021-07-01 10:23:23 | 来源: | 分类:字符串 函数

      示例1
<?php 
if (str_contains('abc', '')) {
    echo "Checking the existence of the empty string will always return true";
}
?>

      示例2
<?php 
$string = 'The lazy fox jumped over the fence';
if (str_contains($string, 'lazy')) {
    echo "The string 'lazy' was found in the string\n";
}
if (str_contains($string, 'Lazy')) {
    echo 'The string "Lazy" was found in the string';
}
 else {
    echo '"Lazy" was not found because the case does not match';
}
?>

阅读全文 »

字符串 函数 str_pad 使用另一个字符串填充字符串为指定长度

发表日期:2021-07-01 10:23:23 | 来源: | 分类:字符串 函数

      示例1
<?php 
$input = "Alien";
echo str_pad($input, 10);
                      // 输出 "Alien     "echo str_pad($input, 10, "-=", STR_PAD_LEFT);
  // 输出 "-=-=-Alien"echo str_pad($input, 10, "_", STR_PAD_BOTH);
   // 输出 "__Alien___"echo str_pad($input,  6, "___");
               // 输出 "Alien_"echo str_pad($input,  3, "*");
                 // 输出 "Alien"?>

阅读全文 »

字符串 函数 str_repeat 重复一个字符串

发表日期:2021-07-01 10:23:23 | 来源: | 分类:字符串 函数

      示例1
<?php 
echo str_repeat("-=", 10);
?>

阅读全文 »

字符串 函数 str_replace 子字符串替换

发表日期:2021-07-01 10:23:23 | 来源: | 分类:字符串 函数

      示例1
<?php 
// 赋值: <body text='black'>$bodytag = str_replace("%body%", "black", "<body text='%body%'>");
// 赋值: Hll Wrld f PHP$vowels = array("a", "e", "i", "o", "u", "A", "E", "I", "O", "U");
$onlyconsonants = str_replace($vowels, "", "Hello World of PHP");
// 赋值: You should eat pizza, beer, and ice cream every day$phrase  = "You should eat fruits, vegetables, and fiber every day.";
$healthy = array("fruits", "vegetables", "fiber");
$yummy   = array("pizza", "beer", "ice cream");
$newphrase = str_replace($healthy, $yummy, $phrase);
// 赋值: 2$str = str_replace("ll", "", "good golly miss molly!", $count);
echo $count;
?>

      示例2
<?php 
// 替换顺序$str     = "Line 1\nLine 2\rLine 3\r\nLine 4\n";
$order   = array("\r\n", "\n", "\r");
$replace = '<br />';
// 首先替换 \r\n 字符,因此它们不会被两次转换$newstr = str_replace($order, $replace, $str);
// 输出 F ,因为 A 被 B 替换,B 又被 C 替换,以此类推...// 由于从左到右依次替换,最终 E 被 F 替换$search  = array('A', 'B', 'C', 'D', 'E');
$replace = array('B', 'C', 'D', 'E', 'F');
$subject = 'A';
echo str_replace($search, $replace, $subject);
// 输出: apearpearle pear// 由于上面提到的原因$letters = array('a', 'p');
$fruit   = array('apple', 'pear');
$text    = 'a p';
$output  = str_replace($letters, $fruit, $text);
echo $output;
?>

阅读全文 »

字符串 函数 str_rot13 对字符串执行 ROT13 转换

发表日期:2021-07-01 10:23:23 | 来源: | 分类:字符串 函数

      示例1
<?php 
echo str_rot13('PHP 4.3.0');
 // CUC 4.3.0?>

阅读全文 »

字符串 函数 str_shuffle 随机打乱一个字符串

发表日期:2021-07-01 10:23:23 | 来源: | 分类:字符串 函数

      示例1
<?php 
$str = 'abcdef';
$shuffled = str_shuffle($str);
// 输出类似于: bfdaececho $shuffled;
?>

阅读全文 »

字符串 函数 str_ireplace str_replace() 的忽略大小写版本

发表日期:2021-07-01 10:23:23 | 来源: | 分类:字符串 函数

      示例1
<?php 
$bodytag = str_ireplace("%body%", "black", "<body text=%BODY%>");
echo $bodytag;
 // <body text=black>?>

阅读全文 »

字符串 函数 str_word_count 返回字符串中单词的使用情况

发表日期:2021-07-01 10:23:23 | 来源: | 分类:字符串 函数

      示例1
<?php 
$str = "Hello fri3nd, you're       looking          good today!";
print_r(str_word_count($str, 1));
print_r(str_word_count($str, 2));
print_r(str_word_count($str, 1, 'àáãç3'));
echo str_word_count($str);
?>

阅读全文 »

字符串 函数 md5 计算字符串的 MD5 散列值

发表日期:2021-07-01 10:23:22 | 来源: | 分类:字符串 函数

      示例1
<?php 
$str = 'apple';
if (md5($str) === '1f3870be274f6c49b3e31a0c6728957f') {
    echo "Would you like a green or red apple?";
}
?>

阅读全文 »

全部博文(1580)
集速网 copyRight © 2015-2022 宁ICP备15000399号-1 宁公网安备 64010402001209号
与其临渊羡鱼,不如退而结网
欢迎转载、分享、引用、推荐、收藏。