PHP 程序查找句子的第一个单词

phpserver side programmingprogramming更新于 2025/8/23 3:22:17

要查找句子的第一个单词,PHP 代码如下 −

示例

<?php
   $my_string = 'Hi there, this is a sample statement';
   echo "The first word of the string is ". strtok($my_string, " ");
?>

输出

The first word of the string is Hi

首先定义一个字符串 −

$my_string = 'Hi there, this is a sample statement';

‘strtok’ 函数是一个内置函数,用于将字符串拆分成指定数量的部分。在第一个空格出现之前,字符串需要被拆分。拆分后的字符串的第一部分将作为输出显示在屏幕上。 −

echo "The first word of the string is ". strtok($my_string, " ");

相关文章