PHP学习笔记 05 - 字符串

本文介绍了PHP中常用的字符串处理函数,包括获取字符串长度、单词数量、字符串逆序、查找子串及替换等操作,并通过示例代码展示了这些函数的具体用法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

字符串函数

函数说明
strlen(string)获取字符串的长度
str_word_count(string)获取字符串中单词个数
strrev(string)字符串逆序
strpos(string,find,start)在string中查找find,从start开始(可选)
str_replace(find,replace,string,count)将string中的find替换为replace, count表示替换的次数(可选)

示例

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
    <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
    <title>Hello World!</title>
</head>

<body>
    <div class="well">
    <?php

    $greeting = "Hello world!";

    # 获取字符串长度
    echo "The length of \"$greeting\" is " . strlen($greeting) . "<br>";

    # 获取单词个数
    echo "The count of words of \"$greeting\" is " . str_word_count($greeting) . "<br>";

    # 逆序
    echo "Reverse \"$greeting\" we get \"" . strrev($greeting) . "\"<br>";

    # 查找
    echo "Position of \"world\" in \"$greeting\" is " . strpos($greeting, "world") . "<br>";

    # 替换
    echo "Repalce \"world\" of \"$greeting\" with \"work\" we get \"" . str_replace("world", "work", $greeting) . "\"<br>";

    ?>
    </div>
</body>

</html>

查看运行结果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值