preg_split() is a PHP function which allows splitting a string using a regular expression.
Questions tagged [preg-split]
550 questions
-1
votes
2 answers
How to get only operands with regex
I am trying to extract only operands viz. + - / and * from my arithmetic expression.
E.g.: A + B should return me +
I tried using following few RegEx but I always get an array with 3 elements:
Expression #1:
print_r (var_dump(preg_split (…

Akshay Lokur
- 6,680
- 13
- 43
- 62
-1
votes
2 answers
PHP preg_split() - don't split spaces between ' '
I have this string:
$string = "My name is Emma and i have a dillemma, what's the distance between 'New York' and 'Athene' ?";
I'm splitting this string by space and some operators(=,<,>,!=,>=,<=,<>) using this code:
$split =…

emma
- 761
- 5
- 20
-1
votes
2 answers
PHP Splitting string using preg_split
I am trying to parse a formula structure that I have created. So far It's really close. I just need some help with the regex to split the equal sign out.
Here's the string I'm testing
({1+1=2|2+2=4}+{1+2=3|2+3=5})=16+10
This is the output i'm…

joeb
- 777
- 8
- 28
-1
votes
1 answer
Storing an array in the database with preg_split
I have a textarea where I can enter as many items as I want separated by a line break.
Then, in the php file I have this to get the values:
$colors = $_POST["colors"];
foreach ( preg_split ('/[\s*,\s*]*,+[\s*,\s*]*/', $colors) as $value)
{
…

saysmus
- 81
- 1
- 1
- 12
-1
votes
1 answer
preg_split dealing with ' in a surname using php
I am importing a text file with marks for each student into my MySQL database. I am using preg_split to read each line and break the info into separate data which is working except for students with a surname containing a ['] such as O'Neil.…

Vincent
- 83
- 1
- 1
- 9
-1
votes
3 answers
PHP preg_split curly brackets
Who can help me out?
I have a string like this:
$string = '
{titleInformation}
'; I want to split this string so that I get the following array: array ( 0 => '
', 1 => '{titleInformation}', 2 => '
', ) I'm new to regular…

R. Haamke
- 11
- 1
- 1
-1
votes
2 answers
preg_split : including empty field
I want to split a TSV string. The structure is:
abc\tdef\tghi\tjklm
where \t is a tab character.
If I use preg_split to split such string $i
$field=preg_split("/\t/", $i);
$field[3] is jklm.
However, if I have another…

Nissa
- 215
- 1
- 7
-1
votes
1 answer
How to split command by & && || ; and | in php that will work with quotes
I have this function to validate command in php?
public function validate_command($command) {
if (isset($this->config->settings->guest_commands)) {
$commands = $this->config->settings->guest_commands;
} else {
$commands =…

jcubic
- 61,973
- 54
- 229
- 402
-1
votes
3 answers
split a string by multiple delimeters, and one of them is +
EDIT: I was taking this too literally- a commenter correctly pointed out that the + in the querystring is just a url encoding for a space- sorry for the confusion
I need to split a string by a couple delimiters (, and +). The background is I'm…

Diane Kaplan
- 1,626
- 3
- 24
- 34
-1
votes
2 answers
Split string into associative array
Example string (html content):
some content
title 1
more content
title 2
rest of the content I need to split this into associative array by the , yet keep all contents of the string. Desired outputs: array(){ …
Matija Mrkaic
- 1,817
- 21
- 29
-1
votes
2 answers
I am having an issue with preg_split syntax
I am doing some tests moving from the function split to preg_split
in this line of code
list($tag) = preg_split('/[ >]/', substr($chunk, 2 - $open), 2);
What exactly do the [] do? Can they be omitted?

Mohamed Farouk
- 41
- 1
- 7
-1
votes
1 answer
Preg_split & Regex - Removing brackets, text and colons
I am trying to understand how to split a string using a regex and preg_split, but I can't seem to understand the explanations online.
Especially the selector for the brackets seems to be impossible to create.
I am trying to do 2 different kinds of…

PIDZB
- 903
- 1
- 15
- 38
-1
votes
2 answers
Keeping the punctuation marks after using preg_split at?
I'm trying to split a string at question marks, exclamation marks, or periods, but at the same time I'm trying to keep the punctuation marks after splitting them. How would I do that? Thanks.
$input = "Sentence1?Sentence2.Sentence3!";
$input =…

jessica
- 1,667
- 1
- 17
- 35
-1
votes
3 answers
extract information in between the * symbol of the text and save each data into a separate array
I have a text file as "a.txt",
which has a data like the image. I want to read this text file line by line and save each entry between the * symbol and save into a separate array.
Note: each information I want to save into a separate array.
I…

ombioinfo
- 33
- 4
-1
votes
3 answers
Custom regular expression pattern
What's the right pattern to obtain something like that using preg_split.
Input:
Src.[VALUE1] + abs(Src.[VALUE2])
Output:
Array (
[0] => Src.[VALUE1]
[1] => Src.[VALUE2]
)

user3383611
- 3
- 2