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

PhpPhp 2023-09-01 04:46:16 921
摘要: Createafunctiontocheckwhetherastringbeginswiththespecifiedstringornot.ThefunctionshouldreturnTRUEonsuccessorFALSEonfailure.Thefollowingisthesyntax−begnWith(str,beg...

在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中检查一个字符串是否以给定的单词开头的详细内容,更多请关注其它相关文章!