Home >Backend Development >PHP Problem >How to determine whether a string contains dots in php
3 methods: 1. Use stripos(), the syntax is "stripos($str, ".")", and the returned position value is included. 2. Use strripos(), the syntax is "strripos($str, ".")", and the returned position value is included. 3. Use explode() with the syntax "explode($str,".")". If the returned array is an empty array, it will not be included. Otherwise, it will be included.
The operating environment of this tutorial: windows7 system, PHP version 8.1, DELL G3 computer
php detection string Methods to determine whether a string contains specified characters
Method 1: Use the stripos() function to detect
The stripos() function can find a string in another string The position of the first occurrence in a string (case-insensitive).
If the specified character exists, return the position of the first occurrence; if not found, return FALSE.
Note: The string position starts from 0, not from 1.
Example:
<?php header('content-type:text/html;charset=utf-8'); $str = '12.36'; $pos = stripos($str, "."); var_dump($pos); if($pos){ echo $str." 中指定字符 圆点.<br>"; }else{ echo $str." 中不包含指定字符 圆点.<br>"; } ?>
Method 2: Use strripos() function to detect
strripos() function can Finds the last occurrence of a string within another string (case-insensitive).
If the specified character exists, return the position of the last occurrence; if not found, return FALSE.
Example:
<?php header('content-type:text/html;charset=utf-8'); $str = '12.36.3'; $pos = strripos($str, "."); var_dump($pos); if($pos){ echo $str." 中指定字符 圆点.<br>"; }else{ echo $str." 中不包含指定字符 圆点.<br>"; } ?>
Method 3: Use the explode() function to detect
explode() function usage Specifies the delimiter to split a string and returns an array of strings.
If the array is not an empty array (the array length is greater than 1), it is included; otherwise, the array is empty and does not contain another string
<?php header('content-type:text/html;charset=utf-8'); $url = "001a.gif"; $str = "."; $con = explode($str,$url); if (count($con)>1){ echo $url." 中包含 圆点".$str; }else{ echo $url." 中没有包含圆点 ".$str; } ?>
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to determine whether a string contains dots in php. For more information, please follow other related articles on the PHP Chinese website!