PHP关于字符串的各类处理方法

news/2024/10/23 7:27:56/

判断字符串是否以指定子串开头或结尾

function startsWith($str, $prefix) {return stripos($str, $prefix) === 0;
}function endsWith($str, $suffix) {return substr_compare($str, $suffix, -strlen($suffix)) === 0;
}// 示例用法
$text = "hello world";
$result = startsWith($text, "he");
echo $result;  // 输出结果: true$text = "hello world";
$result = endsWith($text, "ld");
echo $result;  // 输出结果: true

 统计字符串中指定子串出现的次数

function countSubstring($str, $substring) {return substr_count($str, $substring);
}// 示例用法
$text = "Hello, hello world!";
$result = countSubstring($text, "hello");
echo $result;  // 输出结果: 2

检查字符串是否为空或只包含空白字符

function isStringEmpty($str) {return trim($str) === "";
}// 示例用法
$text = "  ";
$result = isStringEmpty($text);
echo $result;  // 输出结果: true

格式化字符串为驼峰命名法

function toCamelCase($str) {$str = ucwords(str_replace(['-', '_'], ' ', $str));return lcfirst(str_replace(' ', '', $str));
}// 示例用法
$text = "hello-world";
$result = toCamelCase($text);
echo $result;  // 输出结果: helloWorld

检查字符串是否是回文

function isPalindrome($str) {$str = strtolower(preg_replace('/[^a-zA-Z0-9]/', '', $str));return $str === strrev($str);
}// 示例用法
$text = 'A man, a plan, a canal: Panama.';
$result = isPalindrome($text);
echo $result ? '是回文' : '不是回文';  // 输出结果: 是回文

提取字符串中的数字

function extractNumbers($str) {preg_match_all('/\d+/', $str, $matches);return implode('', $matches[0]);
}// 示例用法
$text = 'abc123def456';
$result = extractNumbers($text);
echo $result;  // 输出结果: 123456

翻转字符串中的单词顺序

function reverseWords($str) {return implode(' ', array_reverse(explode(' ', $str)));
}// 示例用法
$text = 'Hello world, this is PHP.';
$result = reverseWords($text);
echo $result;  // 输出结果: PHP. is this world, Hello

删除字符串中的空格

function removeSpaces($str) {return str_replace(' ', '', $str);
}// 示例用法
$text = 'Hello, World!';
$result = removeSpaces($text);
echo $result;  // 输出结果: Hello,World!

替换字符串中的特定内容

function replaceString($str, $search, $replace) {return str_replace($search, $replace, $str);
}// 示例用法
$text = "hello world";
$result = replaceString($text, "world", "everyone");
echo $result;  // 输出结果: hello everyone


http://www.ppmy.cn/news/979674.html

相关文章

【N32L40X】学习笔记03-gpio输出库

gpio输出 该函数库的目的就是在统一的地方配置&#xff0c;将配置的不同项放置在一个结构体内部使用一个枚举来定义一个的别名 led.c #include <stdio.h> #include "led/bsp_led.h"static led_t leds[LED_NUM]{{GPIOB,GPIO_PIN_2,RCC_APB2_PERIPH_GPIOB},{GP…

3秒快速打开 jupyter notebook

利用 bat 脚本&#xff0c;实现一键打开 minconda 特点&#xff1a; 1、可指定 python 环境 2、可指定 jupyter 目录 一、配置环境 minconda 可以搭建不同的 python 环境&#xff0c;所以我们需要找到 minconda 安装目录&#xff0c;把对应目录添加到电脑环境 PATH 中&#…

ADCS知识点全收录! 如何利用证书服务器对域控进行多角度攻击(上)

在2021年的BlackHat大会上&#xff0c;由Will Schroeder和Lee Christensen发布了关于Active Directory Certificate Services 利用白皮书《Certified Pre-Owned - Abusing Active Directory Certificate Services》&#xff0c;其中包含了大量的针对ADCS的攻击手法&#xff0c;…

离多态更近一步

在面向对象的语言里面,封装,继承,多态可谓是在熟悉不过了,当我们每次再去重新认识它们的时候总会有新的发现,为此我也经常感到疑惑,所以在这里和大家一起探讨三个问题,让我们在向多态靠近一点点。 虚表是否真的存在静态区 经常我们都会看见一个问题&#xff0c;虚表到底是存放…

【Redis】Redis zset 实现排行榜

文章目录 前言案例程序设计score 设计 (相同积分的排序)缓存数据定时刷新当心缓存击穿 前言 排行榜是业务开发中常见的一个场景&#xff0c;如何设计一个好的数据结构能够满足高效实时的查询&#xff0c;下面我们结合一个实际例子来讨论一下。 案例 大概需求就是: 排行榜上显…

20款奔驰S350商务型加装原厂前排座椅通风系统,夏天必备的功能

通风座椅的主动通风功能可以迅速将座椅表面温度降至适宜程度&#xff0c;从而确保最佳座椅舒适性。该功能启用后&#xff0c;车内空气透过打孔皮饰座套被吸入座椅内部&#xff0c;持续时间为 8 分钟。然后&#xff0c;风扇会自动改变旋转方向&#xff0c;将更凉爽的环境空气从座…

16K个大语言模型的进化树;81个在线可玩的AI游戏;AI提示工程的终极指南;音频Transformers课程 | ShowMeAI日报

&#x1f440;日报&周刊合集 | &#x1f3a1;生产力工具与行业应用大全 | &#x1f9e1; 点赞关注评论拜托啦&#xff01; &#x1f916; LLM 进化树升级版&#xff01;清晰展示 15821 个大语言模型的关系 这张进化图来自于论文 「On the Origin of LLMs: An Evolutionary …

【C++ 进阶】继承

一.继承的定义格式 基类又叫父类&#xff0c;派生类又叫子类&#xff1b; 二.继承方式 继承方式分为三种&#xff1a; 1.public继承 2.protected继承 3.private继承 基类成员与继承方式的关系共有9种&#xff0c;见下表&#xff1a; 虽然说是有9种&#xff0c;但其实最常用的还…