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
15
votes
3 answers

Split a text by a backslash \ ?

I've searched for hours. How can I separate a string by a "\" I need to separate HORSE\COW into two words and lose the backslash.
user723220
  • 817
  • 3
  • 12
  • 20
15
votes
10 answers

Validate that input string does not exceed word limit

I want to count the words in a specific string so that I can validate it and prevent users to write more than, for example, 100 words. I wrote this function, but I don't think it's effective enough. I used the explode function with space as a…
Waseem Senjer
  • 1,026
  • 3
  • 14
  • 25
15
votes
4 answers

"Empty delimiter" Warning when using PHP explode() function

In javascript, var myStringToArray = myString.split(''); is perfectly acceptable. But in PHP, $My_String_To_Array = explode('', $My_String); throws an error: Warning: explode() Empty delimiter The PHP manual…
Rounin
  • 27,134
  • 9
  • 83
  • 108
15
votes
8 answers

Explode a paragraph into sentences in PHP

I have been using explode(".",$mystring) to split a paragraph into sentences. However this doen't cover sentences that have been concluded with different punctuation such as ! ? : ; Is there a way of using an array as a delimiter instead of a…
Chris Headleand
  • 6,003
  • 16
  • 51
  • 69
14
votes
1 answer

In PHP, how can I split a string by whitespace, commas, and newlines at the same time

Possible Duplicate: How to split a string by multiple delimiters in PHP? What would be the most efficient way to split a string by three delimiters at the same time, specifically a single white space, newline, and commas?
Michael Staudt
  • 327
  • 2
  • 5
  • 13
13
votes
6 answers

Split String Into Array and Append Prev Value

I have this string: var/log/file.log I eventually want to end up with an array looking like this: Array => [ '1' => 'var', '2' => 'var/log', '3' => 'var/log/file.log' ] I currently have this:
treyBake
  • 6,440
  • 6
  • 26
  • 57
13
votes
3 answers

php explode new lines content from a txt file

I have txt file with email addresses under under the other like : test@test.com test2@test.com So far I managed to open it with $result = file_get_contents("tmp/emails.txt"); but I don't know to to get the email addresses in an array. Basically I…
Michael
  • 6,377
  • 14
  • 59
  • 91
13
votes
3 answers

If you explode a string and said string does not contain the delimiter, does explode kick an error?

So I'm getting a section of the url with php. This section may or may not have trailing '?' values. So for example : I'm exploding the url : stackoverflow.com/questions and I want to get questions so i do something like explode ('/',$url). Pretend…
Edward G-Jones
  • 575
  • 3
  • 11
  • 24
12
votes
11 answers

Split a string on the last occurring space

I need to split a string into two parts. The string contains words separated by a space and can contain any number of words, e.g: $string = "one two three four five"; The first part needs to contain all of the words except for the last word. The…
MAX POWER
  • 5,213
  • 15
  • 89
  • 141
12
votes
2 answers

How to explode a multi-line string?

I have a string that has different values on each line: $matches="value1 value2 value3 value4 value5 "; I want to explode the whole string in to an array consisting of the values separeted. I know how to explode a space separated string, like…
Sujit Agarwal
  • 12,348
  • 11
  • 48
  • 79
12
votes
3 answers

PHP case-insensitive explode()

I have the following code: explode("delimiter", $snippet); But I want that my delimiter is case-insensitive.
Luis Liz
  • 1,939
  • 4
  • 20
  • 31
11
votes
5 answers

php count the number of strings after exploded

Here is my code $key) { $i >0; echo $i.' '.$key .'
'; } ?> the output is 0 a 1 b 2 c 3 d 4 e 5 f What i'm try to count the number of strings after i…
user2203703
  • 1,955
  • 4
  • 22
  • 36
10
votes
1 answer

PHP explode on null character

I am trying to explode a string on what looks like to be a null character. This is what is what I am using: $exp = explode('\x00', $bin);. Though this does not work. However, if I do $exp = explode($bin[5], $bin); (where character 5 of $bin is…
GManz
  • 1,548
  • 2
  • 21
  • 42
10
votes
4 answers

Convert backslash-delimited string into an associative array

I have a string like this: key1\value1\key2\value2\key3\value3\key4\value4\key5\value5 And I'd like it to be an associative array so that I can do: echo $myArray['key1']; // prints value1 echo $myArray['key3']; // prints value3 //etc... I know I…
Pyrite
  • 103
  • 1
  • 1
  • 4
10
votes
1 answer

Regular expression as delimiter in explode()

So I have a string which I'm turning into an array but I want to separate each word using a regex. I'm matching a whole word using the below function. function substr_count_array($haystack, $needle) { $initial = 0; $bits = explode(' ',…
Jessie Stalk
  • 951
  • 3
  • 10
  • 15