php 5string函数了解php 5版本中新增的字符串函数

分类:知识百科 日期: 点击:0
PHP 5版本中新增的字符串函数包括:

str_getcsv()

// 使用 str_getcsv() 将一个 CSV 字符串解析为一个数组
$csv = '"John","Doe","john@example.com","555-123-4567"';
$arr = str_getcsv($csv);
print_r($arr);

// 输出
Array
(
    [0] => John
    [1] => Doe
    [2] => john@example.com
    [3] => 555-123-4567
)

str_ireplace()

// 使用 str_ireplace() 替换字符串中的字符
$str = 'Hello World';
$str = str_ireplace('WORLD', 'PHP', $str);
echo $str;

// 输出
Hello PHP

str_pad()

// 使用 str_pad() 将字符串填充到指定的长度
$str = 'Hello';
$str = str_pad($str, 10, '-', STR_PAD_BOTH);
echo $str;

// 输出
--Hello---

str_repeat()

// 使用 str_repeat() 重复指定字符串
$str = 'Hello';
$str = str_repeat($str, 3);
echo $str;

// 输出
HelloHelloHello

str_shuffle()

// 使用 str_shuffle() 打乱字符串
$str = 'Hello World';
$str = str_shuffle($str);
echo $str;

// 输出
rWoelHl odl

str_split()

// 使用 str_split() 将字符串分割为数组
$str = 'Hello World';
$arr = str_split($str);
print_r($arr);

// 输出
Array
(
    [0] => H
    [1] => e
    [2] => l
    [3] => l
    [4] => o
    [5] =>  
    [6] => W
    [7] => o
    [8] => r
    [9] => l
    [10] => d
)

str_word_count()

// 使用 str_word_count() 统计字符串中的单词数
$str = 'Hello World';
$count = str_word_count($str);
echo $count;

// 输出
2

strip_tags()

// 使用 strip_tags() 删除字符串中的 HTML 标签
$str = '

Hello World

'; $str = strip_tags($str); echo $str; // 输出 Hello World

substr_count()

// 使用 substr_count() 统计字符串中某个子串出现的次数
$str = 'Hello World';
$count = substr_count($str, 'l');
echo $count;

// 输出
3

trim()

// 使用 trim() 删除字符串两端的空白字符
$str = '  Hello World  ';
$str = trim($str);
echo $str;

// 输出
Hello World
标签:

版权声明

1. 本站所有素材,仅限学习交流,仅展示部分内容,如需查看完整内容,请下载原文件。
2. 会员在本站下载的所有素材,只拥有使用权,著作权归原作者所有。
3. 所有素材,未经合法授权,请勿用于商业用途,会员不得以任何形式发布、传播、复制、转售该素材,否则一律封号处理。
4. 如果素材损害你的权益请联系客服QQ:77594475 处理。