How to convert php word string to array

PHPz
Release: 2023-04-24 09:17:18
Original
571 people have browsed it

During the PHP development process, some word strings need to be converted into arrays to facilitate word processing, such as counting the number of word occurrences, sorting words, etc. This article will show you how to quickly and easily convert a word string into an array.

  1. Use the explode() function

The explode() function is a very commonly used string function in PHP. It can cut the string according to a certain delimiter. and returns the cut array. Then we can cut the word string according to spaces to get the word array.

For example:

$str = "This is a sample string";
$word_array = explode(" ", $str);
print_r($word_array);
Copy after login

The output result is:

Array
(
    [0] => This
    [1] => is
    [2] => a
    [3] => sample
    [4] => string
)
Copy after login

As you can see, use the explode() function to cut the string into an array of words, and each word is an array. an element.

  1. Use preg_split() function

Sometimes, the string contains not only spaces, but also other characters, such as punctuation marks, newlines, etc. At this point, using the explode() function cannot meet our needs. At this time, you can use regular expressions to perform splitting, and you need to use the preg_split() function.

The preg_split() function is similar to the explode() function, but it can use regular expressions as separators.

For example:

$str = "This is a sample string. It contains punctuation!";
$word_array = preg_split('/[\s,.!]+/', $str);
print_r($word_array);
Copy after login

The output result is:

Array
(
    [0] => This
    [1] => is
    [2] => a
    [3] => sample
    [4] => string
    [5] => It
    [6] => contains
    [7] => punctuation
)
Copy after login

As you can see, the preg_split() function is used to successfully cut the string into a word array, and the punctuation marks are also removed. .

  1. Clear unnecessary characters

After cutting the string into a word array, sometimes you will find that some unnecessary characters appear in the array, such as spaces and newlines. Talisman and so on. At this time, we need to use the trim() function to clear these characters.

For example:

$str = "This is a sample string.\n It contains white space.  ";
$word_array = preg_split('/[\s,.!]+/', $str);
for ($i=0; $i<count($word_array); $i++) {
    $word_array[$i] = trim($word_array[$i]);
}
print_r($word_array);
Copy after login

The output result is:

Array
(
    [0] => This
    [1] => is
    [2] => a
    [3] => sample
    [4] => string
    [5] => It
    [6] => contains
    [7] => white
    [8] => space
)
Copy after login

As you can see, the trim() function is used to remove useless characters in the array.

Summary:

To convert a word string into an array in PHP, you can use the explode() function and preg_split() function. After using these functions, we also need to clear unnecessary characters in the array. The above three methods can quickly convert strings into arrays, which facilitates our subsequent word processing.

The above is the detailed content of How to convert php word string to array. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template