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

wufei123 发布于 2023-08-21 阅读(1194)

在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 −

1

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

1

2

3

4

5

6

7

8

9

10

<?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!";

?>

输出

以下是输出 −

1

True! It begins with p!


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


发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

大众 新闻23034