当前位置: 首页 > article >正文

PHP substr()函数教程

PHP programming languages provide a lot of different types of functions for string manipulation. In this tutorial, we will look at how to find some part of the given specified string or text. We will use the popular function named substr() .

PHP编程语言为字符串操作提供了许多不同类型的功能。 在本教程中,我们将研究如何查找给定的指定字符串或文本的某些部分。 我们将使用名为substr()的流行函数。

PHP substr()语法 (PHP substr() Syntax)

We will start with the syntax of the substr() function. This function has 3 parameters. This function will return a string which is a substring of the given string.

我们将从substr()函数的语法开始。 此功能有3个参数。 此函数将返回一个字符串,该字符串是给定字符串的子字符串。

string substr(string $string, int $start,[int $length])

As we can see length is optional parameter but $string  and $start are required parameters.

如我们所见,length是可选参数,但是$string$start是必需参数。

  • $string hold the string we want to get some part of.

    $string保留我们想要获取的一部分的字符串。

  • $index is the index value we want to start the substring selection.

    $index是我们要开始选择子字符串的索引值。

  • $length is the count of chracters from $index

    $length是来自$index $length

从开始返回给定长度(Return Given Length From Start)

We will start with a simple example. In this example we will provide the index we want to start and continue to the end which will be a substring. We will start at index 3. Keep in mind that the index starting from 0 which means P is index 0. The following example will print tut.com .

我们将从一个简单的例子开始。 在此示例中,我们将提供我们要开始并继续到结尾的indexindex将是一个子字符串。 我们将从索引3开始。请记住,索引从0开始,这意味着P是索引0。以下示例将打印tut.com

<?php$str="Poftut.com";echo substr($str,3); //Will print "tut.com"?>

从指定索引返回给定长度 (Return Given Length From Specified Index)

In this example, we will provide the index and the length of substring we want to return. So we will provide the optional length parameter. We will get substring which starts at index 3 and length 3. This will return tut.

在此示例中,我们将提供索引和要返回的子字符串的长度。 因此,我们将提供可选的length参数。 我们将获得以索引3和长度3开头的子字符串。它将返回tut

<?php$str="Poftut.com";echo substr($str,3,3); //Will print "tut"?>

从结束或反向返回给定长度 (Return Given Length From End or Reverse)

We can use negative values for index and length. This will run reverse operation. So first we will start from a negative index. In this example, we will start from -3 to the end. This will print only com.

我们可以对索引和长度使用负值。 这将反向运行。 因此,首先我们将从负索引开始。 在此示例中,我们将从-3开始到最后。 这只会打印com

<?php$str="Poftut.com";echo substr($str,-3); //Will print "com"?>

从指定索引返回给定长度或反向 (Return Given Length From Specified Index or Reverse)

We can also provide length as a negative number which will count in a reverse manner. In this example, we will start from 2 but count reverse with -3 . This will return Pof

我们还可以将长度提供为负数,这将以相反的方式计数。 在此示例中,我们将从2开始,但从-3开始倒数。 这将返回Pof

<?php$str="Poftut.com";echo substr($str,2,-3); //Will print "Pof"?>

翻译自: https://www.poftut.com/returning-portion-of-specified-string-in-php-with-substr-function/

http://www.lryc.cn/news/2420242.html

相关文章:

  • STM32H563 LWIP裸机移植 (包含源代码 一文搞定 )
  • Ehcache和Redis封装接口(支持缓存切换)
  • 高性能并发队列Disruptor使用详解,详细解析Disruptor框架的应用和基本原理
  • C/C++中随机函数rand()和srand()的用法
  • static routing
  • 搭建论坛那些小事儿(phpwind、wordpress)
  • SVN(subversion)-简单介绍
  • java过滤字符串前后特殊空格字符 NBSP
  • WSDL、详解
  • 浅谈反编译在项目中的使用
  • 一篇文章搞懂,CCM、DCM、BCM
  • Python中datetime ,date ,time 各有什么区别和优缺点
  • 打造全世界最强大的高性能公链|Solana 如何直指公链领航者之位?
  • 管理项目SourceForge的方法1
  • sdfsdfsdfsdfdsfdsfsdfsdfsdfdsfsdfsd
  • 自己动手写处理器之第四阶段(1)——第一条指令ori的实现
  • 十进制转二进制
  • jacob 用法
  • 简明 SQL 去重指南:掌握 DISTINCT 实现数据去重
  • 【win】系统工具Win PE
  • 响应头Content-disposition的作用及用法
  • VS2015安装
  • Shell中if的使用详解_与||的使用详解
  • PostScript文件
  • 虚拟化 | 使用VMware vCenter Converter快速将计算机从物理环境或其他虚拟化平台迁移到vSphere环境...
  • 各种常见编码的转换算法
  • 数据结构-用大白话讲清楚时间复杂度和空间复杂度
  • 标签特性attribute和元素节点属性property
  • Location 对象
  • 【超级干货】鸟哥笔记VS知乎刘锤:一个运营牛人的进化论