Computer >> คอมพิวเตอร์ >  >> การเขียนโปรแกรม >> PHP

โปรแกรม PHP ค้นหาคำแรกของประโยค


ในการค้นหาคำแรกของประโยค โค้ด 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, " ");