2023-08-20

在PHP中检查一个字符串是否以给定的单词开头

在PHP中检查一个字符串是否以给定的单词开头

Create a function to check whether a string begins with the specified string or not. The function should return TRUE on success or FALSE on failure.

The following is the syntax −

begnWith(str, begnStr)
登录后复制

Consider the following parameters in it to check −

  • str − The string to be tested

  • begnStr − The text to be searched in the beginning of the specified string.

Example

The following is an example −

Live Demo

<?php
   function begnWith($str, $begnString) {
      $len = strlen($begnString);
      return (substr($str, 0, $len) = = = $begnString);
   }
   if(begnWith("pqrstuvwxyz","p"))
      echo "True! It begins with p!";
   else
      echo "False! It isn't beginning with p!";
?>
登录后复制

输出

以下是输出 −

True! It begins with p!
登录后复制

以上就是在PHP中检查一个字符串是否以给定的单词开头的详细内容,更多请关注php中文网其它相关文章!

https://www.php.cn/faq/593997.html

发表回复

Your email address will not be published. Required fields are marked *