查詢句子第一個單詞的 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, " ");

更新日期:17-Aug-2020

645 人次瀏覽

開啟精彩 職業

完成課程獲得證書

立即開始
廣告
© . All rights reserved.