How to Get the First Word of a Sentence in PHP

How to get the first word of a sentence in PHP?

You can use the explode function as follows:

$myvalue = 'Test me more';
$arr = explode(' ',trim($myvalue));
echo $arr[0]; // will print Test

Another example:

$sentence = 'Hello World this is PHP';
$abbreviation = explode(' ', trim($sentence ))[0];
echo $abbreviation // will print Hello

How the first word of the sentence to put in the span

October CMS is using the twig template engine. you can do it in your template:

{{ record.title | split(' ')[0] }}
// for the rest of string
{{ record.title[1:] }}

php first word only from mb string

To make your code work you just need to add the u flag to the regex so that it matches unicode characters:

preg_match('/^\w+/iu', $sentence, $result);  
echo "\nThe first word of string is: ".$result[0];

Output:

The first word of string is: 練馬春日町Ⅳ

Note that since you want the first word you can simply anchor your regex with ^ and the second \b is not required as \w+ will match as many word characters as it can i.e. until it gets to the first word break.

Alternatively you can use mb_split with a regex of \p{Z} which matches any unicode whitespace or invisible separator:

$sentence = '練馬春日町Ⅳ 清掃レポート.pdf'; 
$first_word = mb_split('\p{Z}', $sentence);
echo $first_word[0];

Output:

練馬春日町Ⅳ

Demo on 3v4l.org

extracting the first word of a string using php

You can use list():

list($word, $string) = explode (' ', $string, 2);

But it is already fine. Regular expressions would be overkill in this case.

Get first word from a string, but different dividers

preg_split is what you're looking for.

$str = "bla1 bla2,bla3";
$words = preg_split("/[\s,]+/", $str);

This snippet splits the $str by space, \t, comma, \n.

Get first word of every line

Yes!

$var = substr( $line, 0, strpos( $line, ' ' ) );

substr() trims a string using start position (0, the beginning) and length: http://php.net/manual/en/function.substr.php

We determine the length by using strpos() to find the first occurrence of the search phrase (in this case, a space): http://php.net/manual/en/function.strpos.php

First word of a comma separated sentence php

You can use explode to achieve YOUR RESULT and for IGINORE ' OR " use trim

$str = 'hi my,"name is abc"';
$result = explode(',',$str); //parsing with , as delimiter
$first = explode(' ',$result[0]);
$first = $first[0];

$second = explode(' ',$result[1]);
$second = trim($second[0],"'\"");
$op = $first." ".$second;
echo ucwords($op);

EDIT or if you want it for all , separated values use foreach

$str = 'hi my,"name is abc"';
$result = explode(',',$str); //parsing with , as delimiter
$op = "";
foreach($result as $value)
{
$tmp = explode(' ',$value);
$op .= trim($tmp[0],"'\"")." ";
}
$op = rtrim($op);
echo ucwords($op);

Get the first letter of each word in a string

explode() on the spaces, then you use an appropriate substring method to access the first character of each word.

$words = explode(" ", "Community College District");
$acronym = "";

foreach ($words as $w) {
$acronym .= mb_substr($w, 0, 1);
}

If you have an expectation that multiple spaces may separate words, switch instead to preg_split()

$words = preg_split("/\s+/", "Community College District");

Or if characters other than whitespace delimit words (-,_) for example, use preg_split() as well:

// Delimit by multiple spaces, hyphen, underscore, comma
$words = preg_split("/[\s,_-]+/", "Community College District");

Get first word of a sentence and two characters from second word in PHP?

$myvalue = 'Test some more';
$pos = stripos($myvalue, ' ');
echo substr($myvalue, 0, $pos + 3);

How to select first 10 words of a sentence?

implode(' ', array_slice(explode(' ', $sentence), 0, 10));

To add support for other word breaks like commas and dashes, preg_match gives a quick way and doesn't require splitting the string:

function get_words($sentence, $count = 10) {
preg_match("/(?:\w+(?:\W+|$)){0,$count}/", $sentence, $matches);
return $matches[0];
}

As Pebbl mentions, PHP doesn't handle UTF-8 or Unicode all that well, so if that is a concern then you can replace \w for [^\s,\.;\?\!] and \W for [\s,\.;\?\!].



Related Topics



Leave a reply



Submit