This site uses cookies from Google to deliver its services, to personalize ads and to analyze traffic. Information about your use of this site is shared with Google. By using this site, you agree to its use of cookies. Learn More

How To Get The First Word Of A String/sentence In Php?

How To Get The First Word Of A String/sentence In Php? Ajax,Codeigniter,Jquery,upload How To Get The First Word Of A String/sentence In Php?

There is a string function (strtok) which can be used to split a string into smaller strings (tokens) based on some separator(s). For the purposes of this thread, the first word (defined as anything before the first space character) of Test me more can be obtained by tokenizing the string on the space character.

<?php $value ="Test me more"; echo strtok($value," ");// Test?>
For more details and examples, see the strtok PHP manual page.

Alternative solutions
$myvalue ='Test me more'; echo strstr($myvalue,' ',true); // will print Test
echo current(explode(' ',$myvalue)); // will print Test
$myvalue = 'Test me more'; $arr = explode(' ',trim($myvalue)); echo $arr[0]; // will print Test
$string = ' Test me more '; preg_match('/\b\w+\b/i', $string, $result); echo $result; // will print Test /* You could use [a-zA-Z]+ instead of \w+ if wanted only alphabetical chars. */ $string = ' Test me more '; preg_match('/\b[a-zA-Z]+\b/i', $string, $result); echo $result; // will print Test
Ref: http://stackoverflow.com/questions/2476789/how-to-get-the-first-word-of-a-sentence-in-php

0 Response to "How To Get The First Word Of A String/sentence In Php?"

Posting Komentar

Contact

Nama

Email *

Pesan *