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 )
1992 questions
298
votes
6 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
  • 21,638
  • 25
  • 75
  • 108
269
votes
15 answers

Convert array values from string to int?

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,272
  • 3
  • 17
  • 20
187
votes
11 answers

How can I explode and trim whitespace?

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
  • 15,457
  • 7
  • 48
  • 82
164
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…
OHLÁLÁ
  • 9,372
  • 18
  • 75
  • 118
149
votes
10 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
  • 46,241
  • 67
  • 190
  • 289
78
votes
7 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,067
  • 2
  • 11
  • 15
75
votes
9 answers

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

I have a string such as: "0123456789" and 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? I…
oni-kun
  • 1,685
  • 6
  • 17
  • 22
49
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
  • 30,576
  • 24
  • 107
  • 179
49
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
  • 493
  • 1
  • 4
  • 4
43
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
  • 567
  • 1
  • 4
  • 9
37
votes
13 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,441
  • 6
  • 27
  • 23
37
votes
2 answers

How to go to next record in foreach loop

foreach ($arr as $a1){ $getd=explode(",",$a1); $b1=$getd[0]; } In above code, if that $getd[0] is empty i want to go to next record.
Aryan
  • 375
  • 1
  • 3
  • 6
34
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,051
  • 4
  • 12
  • 19
32
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
  • 15,874
  • 66
  • 193
  • 332
28
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,219
  • 5
  • 35
  • 65
1
2 3
99 100