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/

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值