Questions tagged [explode]

explode() is a PHP function that splits a string based on a delimiter, returning an array.

explode() is a PHP function that splits a string based on a delimiter, returning an array.

array explode ( string $delimiter , string $string [, int $limit ] )

Example

$text = "a b c d";
print_r(explode(" ", $text));

gives:

Array ( [0] => a [1] => b [2] => c [3] => d )
2123 questions
349
votes
9 answers

Split a comma-delimited string into an array?

I need to split my string input into an array at the commas. Is there a way to explode a comma-separated string into a flat, indexed array? Input: 9,admin@example.com,8 Output: ['9', 'admin@example', '8']
Kevin
  • 23,174
  • 26
  • 81
  • 111
310
votes
17 answers

Convert a comma-delimited string into array of integers?

The following code: $string = "1,2,3" $ids = explode(',', $string); var_dump($ids); Returns the array: array(3) { [0]=> string(1) "1" [1]=> string(1) "2" [2]=> string(1) "3" } I need for the values to be of type int instead of type…
cwal
  • 3,732
  • 3
  • 19
  • 20
217
votes
11 answers

Explode string on commas and trim potential spaces from each value

For example, I would like to create an array from the elements in this string: $str = 'red, green, blue ,orange'; I know you can explode and loop through them and trim: $arr = explode(',', $str); foreach ($arr as $value) { $new_arr[] =…
SeanWM
  • 16,789
  • 7
  • 51
  • 83
188
votes
12 answers

Php multiple delimiters in explode

I have a problem, I have a string array, and I want to explode in different delimiter. For Example $example = 'Appel @ Ratte'; $example2 = 'apple vs ratte' and I need an array which is explode in @ or vs. I already wrote a solution, but If…
Mokus
  • 10,174
  • 18
  • 80
  • 122
162
votes
9 answers

Explode string by one or more spaces or tabs

How can I explode a string by one or more spaces or tabs? Example: A B C D I want to make this an array.
DarthVader
  • 52,984
  • 76
  • 209
  • 300
91
votes
8 answers

Add a prefix to each item of a PHP array

I have a PHP array of numbers, which I would like to prefix with a minus (-). I think through the use of explode and implode it would be possible but my knowledge of php is not possible to actually do it. Any help would be appreciated. Essentially I…
MBL
  • 1,197
  • 2
  • 11
  • 17
79
votes
9 answers

PHP: Split string into array, like explode with no delimiter

I have a string such as: "0123456789" And I need to split each character into an array. I, for the hell of it, tried: explode('', '123545789'); But it gave me the obvious: Warning: No delimiter defined in explode) .. How would I come across this?…
oni-kun
  • 1,775
  • 6
  • 17
  • 22
54
votes
5 answers

PHP explode the string, but treat words in quotes as a single word

How can I explode the following string: Lorem ipsum "dolor sit amet" consectetur "adipiscing elit" dolor into array("Lorem", "ipsum", "dolor sit amet", "consectetur", "adipiscing elit", "dolor") So that the text in quotation is treated as a single…
timofey
  • 543
  • 1
  • 4
  • 4
53
votes
4 answers

php explode all characters

I'm looking for the equivalent of what in js would be 'this is a string'.split('') for PHP. If I try $array = explode('', 'testing'); I get an error Warning: explode() [function.explode]: Empty delimiter in Is there another way to do this?
qwertymk
  • 34,200
  • 28
  • 121
  • 184
48
votes
3 answers

Alternative of php's explode/implode-functions in c#

are there a similar functions to explode/implode in the .net-framework? or do i have to code it by myself?
Sheldon Cooper
  • 617
  • 1
  • 5
  • 9
44
votes
3 answers

Mysql where id is in array

I have a string of ids like 1,2,3,4,5 and I want to be able to list all rows in mysql where the ID is contained in that list. I assumed the easiest way would be to turn the string into an array and then match in ($array) but it doesn't work for me -…
bhttoan
  • 2,641
  • 5
  • 42
  • 71
42
votes
12 answers

Explode string into array with no empty elements?

PHP's explode function returns an array of strings split on some provided substring. It will return empty strings when there are leading, trailing, or consecutive delimiters, like this: var_dump(explode('/', '1/2//3/')); array(5) { [0]=> …
Glenn Moss
  • 6,812
  • 6
  • 29
  • 23
41
votes
2 answers

How to go to next record in foreach loop

In the following code, if $getd[0] is empty, I want to go to the next record. foreach ($arr as $a1) { $getd = explode(',' ,$a1); $b1 = $getd[0]; } How can I achieve it?
Aryan
  • 415
  • 1
  • 4
  • 6
38
votes
4 answers

explode textarea php (at new lines)

can I do: explode("\n", $_POST['thetextarea']); and have it work on all platforms? (The question I am asking is will it ever be \r\n and not just \n") EDIT: I forgot to mention that I am saving $_POST['thetextarea'] to a mysql database VARCHAR 255.…
Chris Muench
  • 17,444
  • 70
  • 209
  • 362
35
votes
2 answers

Multiple explode characters with comma and - (hyphen)

I want to explode a string for all: whitespaces (\n \t etc) comma hyphen (small dash). Like this >> - But this does not work: $keywords = explode("\n\t\r\a,-", "my string"); How to do that?
WhatIsOpenID
  • 1,071
  • 4
  • 12
  • 20
1
2 3
99 100