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, " ");