Php get first 2 words string

  • Login
  • Cart

  • Training
  • Videos
  • Functions
  • Formulas
  • Shortcuts
  • Blog

Php get first 2 words string

Summary 

To extract the nth word in a text string (i.e. a sentence, phrase, or paragraph) you can so with a formula that combines 5 Excel functions: TRIM, MID, SUBSTITUTE, REPT, and LEN. IN the example shown, the formula in D5, copied down, is:

The result in column D is the nth word of the text in column B, where n is given in column C.

Explanation 

In this example, the goal is to extract the nth word from the text string given in column B. Excel does not currently have a function dedicated to this task, so this formula is a workaround. At the core, this formula takes a text string with spaces, and "floods" it with additional space by replacing each space in the original text with a large number of space characters using SUBSTITUTE and REPT.

Note: the number of spaces used in the formula is just an arbitrarily large number that will work in all cases. Using the LEN function to calculate the length of the original text string, and using this as the number of spaces, ensures that the formula will work even for very long words in the original text.

Working from the inside out, the SUBSTITUTE function is used to replace all single spaces with a larger number of spaces:

The REPT function and LEN functions are is used to provide a variable number of spaces:

LEN returns 25, so the REPT function returns a string of 25 spaces:

REPT(" ",25) // returns "                         "

The result from REPT is returned directly to the SUBSTITUTE function:

SUBSTITUTE then replaces every single space in the text from B5 with 25 spaces. You can think of the result at this point as "islands" of words floating in a sea of space :)

="Better                         the                         devil                         you                         know"

The result from SUBSTITUTE is returned directly to the MID function. Then the formula uses the MID function to extract the nth word. The start)_num is worked out with this snippet:

(N-1)*LEN(B5)+1 // returns 51

This code is designed to calculate the correct starting point to extract text, taking into account the fact that we've already flooded the text with extra space characters. We are just repeating the logic used previously to calculate the number of spaces to use between words. The n-1 adjustment is needed because the extra space occurs between words, i.e. the 3rd word occurs after the 2nd block of space, the 4th word occur after the 3rd block of space, etc. The length of the original text string is 25 characters and n is 3, so we have:

The num_chars argument is provided as the original length of B5, which is 25. With this information, the MID function extracts 25 characters, beginning with character 51. The result returned directly to the TRIM function:

TRIM strips the space and returns the final result: "devil".

Alternate formula

It is also possible to extract the nth word from a text string with this formula, based on the FILTERXML function.

Text to Columns

Don't forget that Excel has a built-in Text to Columns feature that can split text according to the delimiter of your choice. If you just need to get the 3rd word from a lot of text strings, this formula may be more convenient (and dynamic), but Text to Columns is still useful in many situations.

Related formulas 

Php get first 2 words string

FIND returns the position (as a number) of the first occurrence of a space character in the text. This position, minus one, is fed into the LEFT function as num_chars. The LEFT function then extracts characters starting at the the left side of the...

Php get first 2 words string

This formula is an interesting example of a "brute force" approach that takes advantage of the fact that TRIM will remove any number of leading spaces. Working from the inside out, we use the SUBSTITUTE function to find all spaces in the text, and...

Php get first 2 words string

Starting from the inside out, the MID function is used to extract all text after "@": MID ( B5 , FIND ( "@" , B5 ), LEN ( B5 )) The FIND function provides the starting point, and for total characters to extract, we just use LEN on the...

Php get first 2 words string

At the core, this formula looks for a line delimiter ("delim") and replaces it with a large number of spaces using the SUBSTITUTE and REPT functions. Note: In older versions of Excel on a Mac, use CHAR(13) instead of CHAR(10). The CHAR function...

Php get first 2 words string

Working from the inside out, the MID function is used to cast the string into an array of individual letters: MID ( B5 , ROW ( INDIRECT ( "1:" & LEN ( B5 ))), 1 ) In this part of the formula, MID , ROW , INDIRECT , and LEN are used to...

Php get first 2 words string

The gist of this formula is to replace a given delimiter with a large number of spaces using SUBSTITUTE and REPT, then use the MID function to extract text related to the "nth occurrence" and the TRIM function to get rid of the extra space. In this...

Php get first 2 words string

Older versions of Excel do not have a function dedicated to splitting text to an array, similar to the PHP explode function, or Python split method. As a workaround, you can use the FILTERXML function, after first adding XML markup to the text. In...

Related functions 

Php get first 2 words string

The Excel TRIM function strips extra spaces from text, leaving only a single space between words and no space characters at the start or end of the text.

Php get first 2 words string

The Excel MID function extracts a given number of characters from the middle of a supplied text string. For example, =MID("apple",2,3) returns "ppl".

Php get first 2 words string

The Excel SUBSTITUTE function replaces text in a given string by matching. For example =SUBSTITUTE("952-455-7865","-","") returns "9524557865"; the dash is stripped. SUBSTITUTE is case-sensitive and does not support wildcards.

Php get first 2 words string

The Excel REPT function repeats characters a given number of times. For example, =REPT("x",5) returns "xxxxx".

Php get first 2 words string

The Excel LEN function returns the length of a given text string as the number of characters. LEN will also count characters in numbers, but number formatting is not included.

Php get first 2 words string

Excel Formula Training

Formulas are the key to getting things done in Excel. In this accelerated training, you'll learn how to use formulas to manipulate text, work with dates and times, lookup values with VLOOKUP and INDEX & MATCH, count and sum with criteria, dynamically rank values, and create dynamic ranges. You'll also learn how to troubleshoot, trace errors, and fix problems. Instant access. See details here.

Download 100+ Important Excel Functions

Get over 100 Excel Functions you should know in one handy PDF.

Excel video training

Quick, clean, and to the point.

Learn more

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

get each word as a row in an array..
reduce that array to the first letter. $acronym = array_reduce( str_word_count("Community College District", 1), function($res , $w){ return $res . $ w[0]; } );.

How can I get only the first word of a string in PHP?

Method 3: Using strstr() Function: The strstr() function is used to search the first occurrence of a string inside another string. This function is case-sensitive. . strstr ( $sentence , ' ' , true);

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

To get the first character from a string, we can use the substr() function by passing 0,1 as second and third arguments in PHP.

How do I limit words in PHP?

function limit_text($text, $limit) { if (str_word_count($text, 0) > $limit) { $words = str_word_count($text, 2); $pos = array_keys($words); $text = substr($text, 0, $pos[$limit]) . '...'; } return $text; } echo limit_text('Hello here is a long sentence that will be truncated by the', 5);