Home > Backend Development > PHP Problem > How to intercept php string

How to intercept php string

藏色散人
Release: 2023-02-24 07:36:01
Original
7517 people have browsed it

How to intercept php string

PHP string interception function

substr function

Description : Implement interception of string

Syntax:

string substr(string $string,int $start [, int $length ])
Copy after login

Description: If length is omitted, the string from start to the end of the character is returned; if start or length is a negative number, it is reciprocal.

$str = 'javascript';
echo substr($str,0,4);
//结果是 java
echo substr($str,4);
echo substr($str,-2);//得到pt
Copy after login

The other one is more special, if the length is a negative number then

$str = 'javascript';
echo substr($str,-5,-2);//得cri 即倒数第五开始到倒数第二的前一位
Copy after login

strstr function

Description: Will search for the first occurrence of a string in another string, case-sensitive

Syntax:

string strstr(string $haystack, mixed $needle)
Copy after login

Description: The function returns the rest of the string

Example:

<?php
    $str1 = "abcdef";
    $str2 = "cd";
    $str3 = strstr($str1,$str2);
    echo $str3;
Copy after login

The output result is cdef

stristr function

Description: Search characters The position of the first occurrence of a string in another string, but not case-sensitive

Syntax:

string stristr(string $haystack, mixed needle)
Copy after login

Related recommendations: "PHP Tutorial"

The above is the detailed content of How to intercept php string. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template