How can i get the first word of a string in php?

Author - Webner

Problem: After the first space in the string no data appears in the input field.

There is an issue when a string contains words with spaces and we want to display the whole string in an input field but after the first space, no data is displayed in the input field.

Solution:This problem occurs when we don’t display the string in double quotes.

For example: The string is:

$var= ”this is the first string”;

In this case, input field shows only “this” and rest of the string is not displayed.

So to overcome this problem we have to use the variable in double quotes and the whole string is displayed as it is.

For example:

$var=”this is the first string”;

This is the right way.

Webner Solutions is a Software Development company focused on developing Insurance Agency Management Systems, Learning Management Systems and Salesforce apps. Contact us at for your Insurance, eLearning and Salesforce applications.

This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters

//extract only first word & convert to lowercase from acf field or sub field
// useful for using as class or ids or datalables to manipluate the dom
//field
//subfield - in repeater

Questions : How to get the first word of a sentence in PHP

2022-09-15T09:01:06+00:00 2022-09-15T09:01:06+00:00

955

I want to extract the first word of a anycodings_text-segmentation variable from a string. For example, take anycodings_text-segmentation this input:


The resultant output should be Test, which anycodings_text-segmentation is the first word of the input. How can I do anycodings_text-segmentation this?

Total Answers 16

24

Answers 1 : of How to get the first word of a sentence in PHP

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


For more details and examples, see the anycodings_text-segmentation strtok PHP manual page.

0

2022-09-15T09:01:06+00:00 2022-09-15T09:01:06+00:00Answer Link

mRahman

6

Answers 2 : of How to get the first word of a sentence in PHP

You can use the explode function as anycodings_text-segmentation 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

0

2022-09-15T09:01:06+00:00 2022-09-15T09:01:06+00:00Answer Link

miraj

5

Answers 3 : of How to get the first word of a sentence in PHP

If you have PHP 5.3

$myvalue = 'Test me more';
echo strstr[$myvalue, ' ', true];

note that if $myvalue is a string with anycodings_text-segmentation one word strstr doesn't return anything anycodings_text-segmentation in this case. A solution could be to anycodings_text-segmentation append a space to the test-string:

echo strstr[ $myvalue . ' ', ' ', true ];

That will always return the first word anycodings_text-segmentation of the string, even if the string has anycodings_text-segmentation just one word in it

The alternative is something like:

$i = strpos[$myvalue, ' '];
echo $i !== false ? $myvalue : substr[ $myvalue, 0, $i ];

Or using explode, which has so many anycodings_text-segmentation answers using it I won't bother pointing anycodings_text-segmentation out how to do it.

0

2022-09-15T09:01:06+00:00 2022-09-15T09:01:06+00:00Answer Link

raja

4

Answers 4 : of How to get the first word of a sentence in PHP

You could do

echo current[explode[' ',$myvalue]];

0

2022-09-15T09:01:06+00:00 2022-09-15T09:01:06+00:00Answer Link

jidam

2

Answers 5 : of How to get the first word of a sentence in PHP

Even though it is little late, but PHP anycodings_text-segmentation has one better solution for this:

$words=str_word_count[$myvalue, 1];
echo $words[0];

0

2022-09-15T09:01:06+00:00 2022-09-15T09:01:06+00:00Answer Link

joy

1

Answers 6 : of How to get the first word of a sentence in PHP

Similar to accepted answer with one less anycodings_text-segmentation step:

$my_value = 'Test me more';
$first_word = explode[' ',trim[$my_value]][0];

//$first_word == 'Test'

0

2022-09-15T09:01:06+00:00 2022-09-15T09:01:06+00:00Answer Link

jidam

5

Answers 7 : of How to get the first word of a sentence in PHP

Just in case you are not sure the string anycodings_text-segmentation starts with a word...

$input = ' Test me more ';
echo preg_replace['/[\s*][[^\s]*][.*]/', '$2', $input]; //Test

0

2022-09-15T09:01:06+00:00 2022-09-15T09:01:06+00:00Answer Link

raja

4

Answers 8 : of How to get the first word of a sentence in PHP


Just use explode to get every word of anycodings_text-segmentation the input and output the first element anycodings_text-segmentation of the resulting array.

0

2022-09-15T09:01:06+00:00 2022-09-15T09:01:06+00:00Answer Link

raja

4

Answers 9 : of How to get the first word of a sentence in PHP

Using split function also you can get anycodings_text-segmentation the first word from string.


0

2022-09-15T09:01:06+00:00 2022-09-15T09:01:06+00:00Answer Link

joy

2

Answers 10 : of How to get the first word of a sentence in PHP

$string = ' Test me more ';
preg_match['/\b\w+\b/i', $string, $result]; // Test
echo $result;

/* 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]; // Test
echo $result;

Regards, Ciul

0

2022-09-15T09:01:06+00:00 2022-09-15T09:01:06+00:00Answer Link

miraj

6

Answers 11 : of How to get the first word of a sentence in PHP

strtok is quicker than extract or preg_* anycodings_text-segmentation functions.

0

2022-09-15T09:01:06+00:00 2022-09-15T09:01:06+00:00Answer Link

joy

5

Answers 12 : of How to get the first word of a sentence in PHP

$input = "Test me more";
echo preg_replace["/\s.*$/","",$input]; // "Test"

0

2022-09-15T09:01:06+00:00 2022-09-15T09:01:06+00:00Answer Link

raja

5

Answers 13 : of How to get the first word of a sentence in PHP

personally strsplit / explode / strtok anycodings_text-segmentation does not support word boundaries, so to anycodings_text-segmentation get a more accute split use regular anycodings_text-segmentation expression with the \w

preg_split['/[\s]+/',$string,1];

This would split words with boundaries anycodings_text-segmentation to a limit of 1.

0

2022-09-15T09:01:06+00:00 2022-09-15T09:01:06+00:00Answer Link

jidam

2

Answers 14 : of How to get the first word of a sentence in PHP

If you want to know how fast each of anycodings_text-segmentation these respective functions is, I ran anycodings_text-segmentation some crude benchmarking in PHP 7.3 on anycodings_text-segmentation the six most voted answers here [strpos anycodings_text-segmentation with substr, explode with current, anycodings_text-segmentation strstr, explode with trim, anycodings_text-segmentation str_word_count and strtok] with anycodings_text-segmentation 1,000,000 iterations each to compare anycodings_text-segmentation their speeds.


Here are the varying results from 2 anycodings_text-segmentation consecutive runs:

strpos/ substr: 6.0736894607544E-8 seconds
strstr: 5.0434112548828E-8 seconds
explode/ current: 3.5163116455078E-7 seconds
explode/ trim: 3.8683795928955E-7 seconds
str_word_count: 4.6665270328522E-6 seconds
strtok: 4.9849510192871E-7 seconds

strpos/ substr: 5.7171106338501E-8 seconds
strstr: 4.7624826431274E-8 seconds
explode/ current: 3.3753299713135E-7 seconds
explode/ trim: 4.2293286323547E-7 seconds
str_word_count: 3.7025549411774E-6 seconds
strtok: 1.2249300479889E-6 seconds

And the results after inverting the anycodings_text-segmentation order of the functions:

strtok: 4.2612719535828E-7 seconds
str_word_count: 4.1899878978729E-6 seconds
explode/ trim: 9.3175292015076E-7 seconds
explode/ current: 7.0811605453491E-7 seconds
strstr: 1.0137891769409E-7 seconds
strpos/ substr: 1.0082197189331E-7 seconds

Conclusion It turns out that the speed anycodings_text-segmentation between these functions varies widely anycodings_text-segmentation and is not as consistent between test anycodings_text-segmentation runs as you might expect. According to anycodings_text-segmentation these quick and dirty tests, any of the anycodings_text-segmentation six chosen functions will get the job anycodings_text-segmentation done in a reasonable amount of time. anycodings_text-segmentation There are perturbations including other anycodings_text-segmentation processes running that are interfering anycodings_text-segmentation with the execution times. So just use anycodings_text-segmentation whatever function makes the most anycodings_text-segmentation practical and readable sense to you as a anycodings_text-segmentation programmer. For the bigger programming anycodings_text-segmentation picture, see Donald Knuth's Literate anycodings_text-segmentation Programming.

0

2022-09-15T09:01:06+00:00 2022-09-15T09:01:06+00:00Answer Link

raja

3

Answers 15 : of How to get the first word of a sentence in PHP

$first_word = str_word_count[1][0]

Doesn't work on special characters, and anycodings_text-segmentation will result in wrong behaviour if anycodings_text-segmentation special characters are used. It is not anycodings_text-segmentation UTF-8 friendly.

For more info check is PHP anycodings_text-segmentation str_word_count[] multibyte safe?

0

2022-09-15T09:01:06+00:00 2022-09-15T09:01:06+00:00Answer Link

raja

1

Answers 16 : of How to get the first word of a sentence in PHP

You question could be reformulated as anycodings_text-segmentation "replace in the string the first space anycodings_text-segmentation and everything following by nothing" . anycodings_text-segmentation So this can be achieved with a simple anycodings_text-segmentation regular expression:

$firstWord = preg_replace["/\s.*/", '', ltrim[$myvalue]];

I have added an optional call to ltrim[] anycodings_text-segmentation to be safe: this function remove spaces anycodings_text-segmentation at the begin of string.

0

2022-09-15T09:01:06+00:00 2022-09-15T09:01:06+00:00Answer Link

joy

How do I get the first letter of a word in PHP?

So you can easily use $i[0] to fetch first letter.

How do I get the first string of a string in PHP?

To get the first n characters from a string, we can use the built-in substr[] function in PHP.

How do I search for a word in text PHP?

Answer: Use the PHP strpos[] Function You can use the PHP strpos[] function to check whether a string contains a specific word or not. The strpos[] function returns the position of the first occurrence of a substring in a string.

How can I get every word in a string in PHP?

PHP str_word_count[] Function.
Count the number of words found in the string "Hello World!": echo str_word_count["Hello world!"]; ... .
Return an array with the words from the string: ... .
Return an array where the key is the position of the word in the string, and value is the actual word: ... .
Without and with the char parameter:.

Bài mới nhất

Chủ Đề