preg_split() is a PHP function which allows splitting a string using a regular expression.
Questions tagged [preg-split]
550 questions
3
votes
2 answers
Excluding array if it contains string
wonder if I can get another pair of eyes on this.
Use case; grabbing DNS records from zone file in order to run shell_exec to import them into another platform.
I am trying to grab all types of DNS records from a zone file, A record, CNAME, TXT, MX…
user3206138
3
votes
2 answers
Split a string at & but not &&
I have a URI that contains both & and && and I need to split it into an array only at the single & delimiter, e.g. query1=X&query2=A&&B&query3=Y
should print out
array(
0 => query1=X
1 => query2=A&&B
2 => query3=Y
)
I know I can use…

Tyssen
- 1,569
- 16
- 35
3
votes
3 answers
Regex for space, but not escaped spaces
I am parsing the input of ls -lb and trying to put each field into an array in PHP.
My input looks something like this:
-rwx------ 1 user.name users 546879 2008-09-05 09:23 Newspaper_Templates.pdf
-rwx------ 1 user.name users 403968…

Rich Bradshaw
- 71,795
- 44
- 182
- 241
3
votes
3 answers
Regular Expressions (Specifically preg_split() PHP)
I'm listing out some dates in my PHP application which results in something like the following:
April2016May2016June2016 etc.
I'm trying to use preg_split to format them like this:
array('April 2016', 'May 2016', 'June 2016')
I used an online…

connorb
- 301
- 4
- 16
3
votes
5 answers
PHP move bold text to array key
I have details array:
[info_details] => Array
(
[0] => title: this is title
[1] => name: this is name
[2] => created this is date
)
and I need to format…

Arnas Pečelis
- 1,000
- 2
- 12
- 32
3
votes
1 answer
How can I explode a string by multiple delimiters and also keep the delimiters?
I'm trying to explode a string using multiple delimiters (↑↑ , ↑ , ↓↓ , ↓).
For example I have this intput string:
$string = "(2.8 , 3.1) → (↓↓2.4 , ↓3.0)";
I would like to convert it into an array like this (expected output):
Array
(
[0] =>…

roullie
- 2,830
- 16
- 26
3
votes
1 answer
PHP mb_split(), capturing delimiters
preg_split has an optional PREG_SPLIT_DELIM_CAPTURE flag, which also returns all delimiters in the returned array. mb_split does not.
Is there any way to split a multibyte string (not just UTF-8, but all kinds) and capture the delimiters?
I'm trying…

Martijn
- 3,696
- 2
- 38
- 64
3
votes
1 answer
Preg_Split delim number and string
I have a problem with preg_split. I need a regex to split my string into number and character. An example of my string is:
1_AB_CD_2_ABC_3_ABD
and I want a result splitted:
1
AB_CD
2
ABC
3
ABD
I've tried with this regex expression but this one not…

Vincenzo Di Roberto
- 55
- 4
3
votes
2 answers
php preg_split a text without loose ,.: and so forth
I try to split a text with preg_split, but I dont get the regrex for it.
example:
I search 1, regex to: no. Or... yes!
should get:
Array
(
[0] => I
[1] => search
[2] => 1
[3] => ,
[4] => regex
[5] => to
[6] => :
[7]…

Thomas
- 1,193
- 1
- 7
- 16
3
votes
1 answer
preg_split by space and tab outside quotes
I am trying to get preg_split() to split the following 2 strings, by space/tab (needs to work on both).
autodiscover.microsoft.com. 3600 IN A 131.107.125.5
and
microsoft.com. 3600 IN TXT "v=spf1 include:_spf-a.microsoft.com…

Sinnbeck
- 617
- 4
- 20
- 56
3
votes
2 answers
PHP Regex - Split string with comma delimiter, IGNORE comma's between tags
Given this string:
$somethingawesome = '<[return date("Y-m-d", strtotime("yesterday"));]>,<[return "cool!";]>,TRUE,foo';
How do I get an array like this:
Array
(
[0] => <[return date("Y-m-d", strtotime("yesterday"));]>
[1] => <[return…

richard
- 12,263
- 23
- 95
- 151
3
votes
1 answer
How do I change a paragraph into an array in PHP including the spaces and punctation
I have a string like this Hello? My name is Ben! @ My age is 32.
I want to change it into an array with all words, spaces and punctuation as separate entities in the array. For example if I did var_dump($sentence) the array should look like…

Ben Paton
- 1,432
- 9
- 35
- 59
3
votes
1 answer
preg_split commas not inside parenthesis
Test string:
Organic whole wheat bread, Monterey Jack Cheese (milk, cheese culture, salt), Hormel Natural Ham (salt, turbinado sugar, lactic acid (not from milk)
Desired output:
Array (
[0] => Organic whole wheat bread
[1] =>…

Stephane
- 1,613
- 5
- 20
- 40
3
votes
2 answers
Preg_split on quote, except when followed by another quote
I'm trying to split a UTF-8 string on a quote character (") with delimiter capture, except where that quote is followed by a second quote ("") so that (for example)
"A ""B"" C" & "D ""E"" F"
will split into three elements
"A ""B"" C"
&
"D ""E""…

Mark Baker
- 209,507
- 32
- 346
- 385
3
votes
1 answer
Convert nested text to strings
I'm at the moment using PHP but I suppose there are other coding languages that would handle this problem better. If you have any suggestions on other coding languages that might work better, please tell me.
I'm trying to convert a nested text to…

fierflash
- 125
- 1
- 6