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

字符串 函数 stripslashes 反引用一个引用字符串

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

      示例1
<?php 
$str = "Is your name O\'reilly?";
// 输出: Is your name O'reilly?echo stripslashes($str);
?>

      示例2
<?php 
function stripslashes_deep($value){
    $value = is_array($value) ?                array_map('stripslashes_deep', $value) :                stripslashes($value);
    return $value;
}
// 范例$array = array("f\\'oo", "b\\'ar", array("fo\\'o", "b\\'ar"));
$array = stripslashes_deep($array);
// 输出print_r($array);
?>

阅读全文 »

字符串 函数 strrev 反转字符串

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

      示例1
<?php 
echo strrev("Hello world!");
 // 输出 "!dlrow olleH"?>

阅读全文 »

字符串 函数 strripos 计算指定字符串在目标字符串中最后一次出现的位置(不区分大小写)

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

      示例1
<?php 
$haystack = 'ababcd';
$needle   = 'aB';
$pos      = strripos($haystack, $needle);
if ($pos === false) {
    echo "Sorry, we did not find ($needle) in ($haystack)";
}
 else {
    echo "Congratulations!\n";
    echo "We found the last ($needle) in ($haystack) at position ($pos)";
}
?>

阅读全文 »

字符串 函数 strrpos 计算指定字符串在目标字符串中最后一次出现的位置

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

      示例1
<?php 
$pos = strrpos($mystring, "b");
if ($pos === false) {
 // 注意: 三个等号    // 未发现...}
?>

      示例2
<?php 
$foo = "0123456789a123456789b123456789c";
var_dump(strrpos($foo, '7', -5));
  // 从尾部第 5 个位置开始查找                                   // 结果: int(17)var_dump(strrpos($foo, '7', 20));
  // 从第 20 个位置开始查找                                   // 结果: int(27)var_dump(strrpos($foo, '7', 28));
  // 结果: bool(false)?>

阅读全文 »

字符串 函数 strstr 查找字符串的首次出现

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

      示例1
<?php 
$email  = 'name@example.com';
$domain = strstr($email, '@');
echo $domain;
 // 打印 @example.com$user = strstr($email, '@', true);
 // 从 PHP 5.3.0 起echo $user;
 // 打印 name?>

阅读全文 »

字符串 函数 strtok 标记分割字符串

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

      示例1
<?php 
$string = "This is\tan example\nstring";
/* 使用制表符和换行符作为分界符 */
$tok = strtok($string, " \n\t");
while ($tok !== false) {
    echo "Word=$tok<br />";
    $tok = strtok(" \n\t");
}
?>

      示例2
<?php 
$first_token  = strtok('/something', '/');
$second_token = strtok('/');
var_dump($first_token, $second_token);
?>

阅读全文 »

字符串 函数 strtoupper 将字符串转化为大写

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

      示例1
<?php 
$str = "Mary Had A Little Lamb and She LOVED It So";
$str = strtoupper($str);
echo $str;
 // 打印 MARY HAD A LITTLE LAMB AND SHE LOVED IT SO?>

阅读全文 »

字符串 函数 strpos 查找字符串首次出现的位置

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

      示例1
<?php 
$mystring = 'abc';
$findme   = 'a';
$pos = strpos($mystring, $findme);
// 注意这里使用的是 ===。简单的 == 不能像我们期待的那样工作,// 因为 'a' 是第 0 位置上的(第一个)字符。if ($pos === false) {
    echo "The string '$findme' was not found in the string '$mystring'";
}
 else {
    echo "The string '$findme' was found in the string '$mystring'";
    echo " and exists at position $pos";
}
?>

      示例2
<?php 
$mystring = 'abc';
$findme   = 'a';
$pos = strpos($mystring, $findme);
// 使用 !== 操作符。使用 != 不能像我们期待的那样工作,// 因为 'a' 的位置是 0。语句 (0 != false) 的结果是 false。if ($pos !== false) {
     echo "The string '$findme' was found in the string '$mystring'";
         echo " and exists at position $pos";
}
 else {
     echo "The string '$findme' was not found in the string '$mystring'";
}
?>

      示例3
<?php 
// 忽视位置偏移量之前的字符进行查找$newstring = 'abcdef abcdef';
$pos = strpos($newstring, 'a', 1);
 // $pos = 7, 不是 0?>

阅读全文 »

字符串 函数 substr_count 计算字串出现的次数

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

      示例1
<?php 
$text = 'This is a test';
echo strlen($text);
 // 14echo substr_count($text, 'is');
 // 2// 字符串被简化为 's is a test',因此输出 1echo substr_count($text, 'is', 3);
// 字符串被简化为 's i',所以输出 0echo substr_count($text, 'is', 3, 3);
// 因为 5+10 > 14,所以生成警告echo substr_count($text, 'is', 5, 10);
// 输出 1,因为该函数不计算重叠字符串$text2 = 'gcdgcdgcd';
echo substr_count($text2, 'gcdgcd');
?>

阅读全文 »

字符串 函数 strspn 计算字符串中全部字符都存在于指定字符集合中的第一段子串的长度。

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

      示例1
<?php 
$var = strspn("42 is the answer to the 128th question.", "1234567890");
?>

      示例2
<?php 
echo strspn("foo", "o", 1, 2);
 // 打印: 2?>

阅读全文 »

字符串 函数 substr_replace 替换字符串的子串

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

      示例1
<?php 
$var = 'ABCDEFGH:/MNRPQR/';
echo "Original: $var<hr />\n";
/* 这两个例子使用 “bob” 替换整个 $var。*/
echo substr_replace($var, 'bob', 0) . "<br />\n";
echo substr_replace($var, 'bob', 0, strlen($var)) . "<br />\n";
/* 将 “bob” 插入到 $var 的开头处。*/
echo substr_replace($var, 'bob', 0, 0) . "<br />\n";
/* 下面两个例子使用 “bob” 替换 $var 中的 “MNRPQR”。*/
echo substr_replace($var, 'bob', 10, -1) . "<br />\n";
echo substr_replace($var, 'bob', -7, -1) . "<br />\n";
/* 从 $var 中删除 “MNRPQR”。*/
echo substr_replace($var, '', 10, -1) . "<br />\n";
?>

      示例2
<?php 
$input = array('A: XXX', 'B: XXX', 'C: XXX');
// A simple case: replace XXX in each string with YYY.echo implode(';
 ', substr_replace($input, 'YYY', 3, 3))."\n";
// A more complicated case where each replacement is different.$replace = array('AAA', 'BBB', 'CCC');
echo implode(';
 ', substr_replace($input, $replace, 3, 3))."\n";
// Replace a different number of characters each time.$length = array(1, 2, 3);
echo implode(';
 ', substr_replace($input, $replace, 3, $length))."\n";
?>

阅读全文 »

字符串 函数 substr_compare 二进制安全比较字符串(从偏移位置比较指定长度)

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

      示例1
<?php 
echo substr_compare("abcde", "bc", 1, 2);
 // 0echo substr_compare("abcde", "de", -2, 2);
 // 0echo substr_compare("abcde", "bcg", 1, 2);
 // 0echo substr_compare("abcde", "BC", 1, 2, true);
 // 0echo substr_compare("abcde", "bc", 1, 3);
 // 1echo substr_compare("abcde", "cd", 1, 2);
 // -1echo substr_compare("abcde", "abc", 5, 1);
 // warning?>

阅读全文 »

字符串 函数 strtolower 将字符串转化为小写

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

      示例1
<?php 
$str = "Mary Had A Little Lamb and She LOVED It So";
$str = strtolower($str);
echo $str;
 // 打印 mary had a little lamb and she loved it so?>

阅读全文 »

字符串 函数 strtr 转换指定字符

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

      示例1
<?php 
$addr = strtr($addr, "äåö", "aao");
?>

      示例2
<?php 
$trans = array("hello" => "hi", "hi" => "hello");
echo strtr("hi all, I said hello", $trans);
?>

      示例3
<?php 
echo strtr("baab", "ab", "01"),"\n";
$trans = array("ab" => "01");
echo strtr("baab", $trans);
?>

阅读全文 »

字符串 函数 trim 去除字符串首尾处的空白字符(或者其他字符)

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

      示例1
<?php 
$text   = "\t\tThese are a few words :) ...  ";
$binary = "\x09Example string\x0A";
$hello  = "Hello World";
var_dump($text, $binary, $hello);
print "\n";
$trimmed = trim($text);
var_dump($trimmed);
$trimmed = trim($text, " \t.");
var_dump($trimmed);
$trimmed = trim($hello, "Hdle");
var_dump($trimmed);
// 清除 $binary 首位的 ASCII 控制字符
// (包括 0-31)
$clean = trim($binary, "\x00..\x1F");
var_dump($clean);
?>
      示例2
<?php 
function trim_value(&$value) {
     $value = trim($value);
 }
$fruit = array('apple','banana ', ' cranberry ');
var_dump($fruit);
array_walk($fruit, 'trim_value');
var_dump($fruit);
?>

阅读全文 »

字符串 函数 str_split 将字符串转换为数组

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

      示例1
<?php 
$str = "Hello Friend";
$arr1 = str_split($str);
$arr2 = str_split($str, 3);
print_r($arr1);
print_r($arr2);
?>

阅读全文 »

字符串 函数 strcasecmp 二进制安全比较字符串(不区分大小写)

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

      示例1
<?php 
$var1 = "Hello";
$var2 = "hello";
if (strcasecmp($var1, $var2) == 0) {
    echo '$var1 is equal to $var2 in a case-insensitive string comparison';
}
?>

阅读全文 »

字符串 函数 str_starts_with Checks if a string starts with a given substring

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

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

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

阅读全文 »

字符串 函数 strcmp 二进制安全字符串比较

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

      示例1
<?php 
$var1 = "Hello";
$var2 = "hello";
if (strcmp($var1, $var2) !== 0) {
    echo '$var1 is not equal to $var2 in a case sensitive string comparison';
}
?>

阅读全文 »

字符串 函数 strchr 别名 strstr()

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

strchr

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

strchr别名 strstr()

说明

此函数是该函数的别名: strstr().

阅读全文 »

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