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
9
votes
2 answers

PHP explode function blank array element

I'm having a few issues with the php explode function. The string i want to explode is: ,.stl,.ppl Currently, i'm using the explode function as such: explode(',',',.stl,.ppl'); Unfortunately this is problematic, it returns three strings: array(3)…
James
  • 349
  • 1
  • 6
  • 17
8
votes
1 answer

PySpark - Add Row Number After Using Explode Function

I'm doing an nlp project and have reviews that contain multiple sentences. I am using the spark-nlp package that outputs one column containing a list of the sentences in each review. I am using explode to create a row for each sentence but I want to…
user3242036
  • 645
  • 1
  • 7
  • 16
8
votes
3 answers

explode two-item-list in array as key=>value

I'd like to explode a multi-line-string like this color:red material:metal to an array like this $array['color']=red $array['material']=metal any idea?
cukabeka
  • 530
  • 2
  • 9
  • 22
8
votes
2 answers

PHP get an array of numbers from string with range

For my current project I need an user to define a range of numbers which is stored in a database. The following string is a possible user input: 1025-1027,1030,1032-1034 I want to process this string with php to get an array of possible numbers…
Stanley Machnitzki
  • 597
  • 1
  • 4
  • 11
8
votes
2 answers

PHP split or explode string on tag

i would like to split a string on a tag into different parts. $string = 'Text other text.'; The next function doesnt work yet on the right way. $array = preg_split('//i', $string); The output should be array( …
8
votes
1 answer

Serialize or Implode

I need to store lots of two-dimensional arrays inside database and was not sure what to use: serialize or implode. So I did a few tests, to find out which one is working faster and came to the conclusion it was serialize: Execution times:…
Peon
  • 7,902
  • 7
  • 59
  • 100
7
votes
4 answers

Javascript - explode equivilent?

So, I have a string with the delimiter | , one of the sections contains "123", is there a way to find this section and print the contents? something like PHP explode (but Javascript) and then a loop to find '123' maybe? :/
Goulash
  • 3,708
  • 6
  • 29
  • 48
7
votes
3 answers

Convert delimited strings into an associative array

I have a file codes.txt with records like this USA 0233 JPN 6789 TUN 8990 CDN 2345 I want to read these content of the file to an associative array like this. $codes["USA"] = 0233; $codes["JPN"] = 6789; $codes["TUN"] = 8990; $codes["CDN"] =…
karto
  • 3,538
  • 8
  • 43
  • 68
7
votes
3 answers

Get first string before separator?

I have strings with folowing structure: 7_string_12 7_string2_122 7_string3_1223 How I can get string before second "_" ? I want my final result to be : 7_string 7_string2 7_string3 I am using explode('_', $string) and combine first two values,…
dido
  • 2,330
  • 8
  • 37
  • 54
7
votes
1 answer

Spark INLINE Vs. LATERAL VIEW EXPLODE differences?

In Spark, for the following use case, I'd like to understand what are the main differences between using the INLINE and EXPLODE ... I'm not sure if there are any performance implications or if one method is preferred over the other one or if there…
dim_user
  • 969
  • 1
  • 13
  • 24
7
votes
5 answers

PHP explode and set to empty string the missing pieces

What's the best way to accomplish the following. I have strings in this format: $s1 = "name1|type1"; //(pipe is the separator) $s2 = "name2|type2"; $s3 = "name3"; //(in some of them type can be missing) Let's assume nameN / typeN are strings and…
Marco Demaio
  • 33,578
  • 33
  • 128
  • 159
7
votes
8 answers

list and explode

I am trying to use url rewriting on my website and I want to use the list() and explode() functions to get the right content. Currently my code looks like this: list($dir, $act) = explode('/',$url); In this case $url is equal to everything after…
cskwrd
  • 2,803
  • 8
  • 38
  • 51
7
votes
6 answers

Why can't I access the exploded array element immediately?

Why can't I immediately access elements in the array returned by explode()? For example, this doesn't work: $username = explode('.',$thread_user)[1]; //Parse error: syntax error, unexpected '[ But this code does: $username =…
Mike Atlas
  • 8,193
  • 4
  • 46
  • 62
7
votes
2 answers

PHP array and implode with blank/null values

I have a array which i generated by values in a database, the example is below: $addressarray = array($results['client']->client_city, $results['client']->client_county, $results['client']->client_postcode); The values are entered by the user using…
snookian
  • 863
  • 6
  • 13
  • 35
7
votes
5 answers

Java (Regex?) split string between number/letter combination

I've been looking through pages and pages of Google results but haven't come across anything that could help me. What I'm trying to do is split a string like Bananas22Apples496Pears3, and break it down into some kind of readable format. Since…
Timr
  • 1,012
  • 1
  • 14
  • 29