Home > Backend Development > PHP Problem > How to query the first occurrence of a string in php

How to query the first occurrence of a string in php

青灯夜游
Release: 2023-03-16 16:54:02
Original
6279 people have browsed it

Two query methods: 1. Use the stripos() function to find the first occurrence of a string in another string in a case-insensitive manner. The syntax "stripos (the searched string, to The string value of the query, the position to start the search)". 2. Use the strpos() function to query the first occurrence position of the case-sensitive search. The syntax is "strpos (string to be searched, string value to be queried, position to start searching)".

How to query the first occurrence of a string in php

The operating environment of this tutorial: windows7 system, PHP version 8.1, DELL G3 computer

php query string Two methods for the first occurrence of the position

Method 1: Use the stripos() function

stripos() function to find the string in another string The position of the first occurrence in a string (case-insensitive).

stripos(string,find,start)
Copy after login
  • string: Required. Specifies the string to be searched for.

  • #find: Required. Specifies the characters to search for.

  • #start: Optional. Specifies the location from which to start the search.

Return value: Returns the position of the first occurrence of a string in another string. If the string is not found, FALSE is returned. Note: The string position starts from 0, not from 1.

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);   
$str="I love php, I love php too!";
echo "原字符串:".$str."<br>";
$find1="php";
echo "指定子串php的出现位置:".stripos($str,$find1)."<br>";
$find2="PHP";
echo "指定子串PHP的出现位置:".stripos($str,$find2);
?>
Copy after login

How to query the first occurrence of a string in php

Method 2: Use the strpos() function

strpos() function to find the first string in another string The position of an occurrence (case sensitive).

strpos(string,find,start)
Copy after login
  • string: Required. Specifies the string to be searched for.

  • #find: Required. Specifies the characters to search for.

  • start: Optional. Specifies the location from which to start the search.

Return value: Returns the position of the first occurrence of a string in another string. If the string is not found, FALSE is returned.

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);   
$str="I love php, I love php too!";
echo "原字符串:".$str."<br>";
$find1="php";
echo "指定子串php的出现位置:".strpos($str,$find1)."<br>";
$find2="PHP";
echo "指定子串PHP的出现位置:".strpos($str,$find2);
?>
Copy after login

How to query the first occurrence of a string in php

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to query the first occurrence of a string in php. 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