preg_split() is a PHP function which allows splitting a string using a regular expression.
Questions tagged [preg-split]
550 questions
4
votes
6 answers
Parse inline style attribute string and get index of a specific attribute name
I have the below php array $tempStyleArray which is created by splitting a string.
$tempStyleArray = preg_split( "/[:;]+/", "width: 569px; height: 26.456692913px; margin: 0px; border: 2px solid black;" );
Produces:
array (
0 => 'width',
1 => '…

chriz
- 1,339
- 2
- 16
- 32
4
votes
5 answers
PHP REGEX - text to array by preg_split at line break
EDITED:
need help on split Array
array example:
array (
[0] =>
:some normal text
:some long text here, and so on... sometimes
i'm breaking down and...
:some normal text
:some…

Luca Filosofi
- 30,905
- 9
- 70
- 77
4
votes
2 answers
preg_split a string by spaces not between single quotes
I am trying to pref_split a string by spaces not between single quotes.
This is the string I am working with:
'physical memory %'=92%;99;100 'physical memory'=29.69GB;31.68;32;0;32
The following regex pattern successfully matches the space that I…

thestepafter
- 598
- 1
- 4
- 20
4
votes
1 answer
Combine regular expressions for splitting camelCase string into words
I managed to implement a function that converts camel case to words, by using the solution suggested by @ridgerunner in this question:
Split camelCase word into words with php preg_match (Regular Expression)
However, I want to also handle embedded…

stou
- 63
- 6
4
votes
3 answers
How can I split a sentence into words and punctuation marks?
For example, I want to split this sentence:
I am a sentence.
Into an array with 5 parts; I, am, a, sentence, and ..
I'm currently using preg_split after trying explode, but I can't seem to find something suitable.
This is what I've…

Lucas
- 16,930
- 31
- 110
- 182
4
votes
1 answer
PHP Regex to match the last occurrence of a string
My string is $text1 = 'A373R12345'
I want to find last none digital number occurrence of this string.
So I use this regular expression ^(.*)[^0-9]([^-]*)
Then I got this result:
1.A373
2.12345
But my expected result is:
1.A373R
(It has…

Nick Hung
- 43
- 1
- 6
4
votes
1 answer
preg_split with two patterns (one of them quoted)
I would like to split a string in PHP containing quoted and unquoted substrings.
Let's say I have the following string:
"this is a string" cat dog "cow"
The splitted array should look like this:
array (
[0] => "this is a string"
[1] => "cat" …

Stefan
- 337
- 6
- 20
4
votes
4 answers
Delimit string by whitespaces
What would be the appropriate regex to delimit a string by all whitespaces? There can also be more than one whitespace between two token and it should ignore whitespaces at the end.
What I have so far:

suamikim
- 5,350
- 9
- 40
- 75
4
votes
3 answers
PHP preg_split with two delimiters unless a delimiter is within quotes
Further on from my previous question about preg_split which was answers super fast, thanks to nick; I would really like to extend the scenario to no split the string when a delimiter is within quotes. For example:
If I have the string foo = bar AND…

Jonathon Oates
- 2,912
- 3
- 37
- 60
4
votes
3 answers
PHP preg_split, expression i curly brackets and saving the delimiters
I've been struggling with this for like half a day already and I can't seem to find the answer. Please do help a noob. :)
I've got a string which consists of few sentences which are in curly brackets. It looks something like this:
{Super duper extra…

moskalak
- 271
- 2
- 12
3
votes
4 answers
preg_split by backslash
In my PHP code, I have class name with namespace assigned in the string, for example:
$my_class_name; // = "Aaa\Bbb\Ccc"; // this is not in source code, just var dump
I need just the middle name, 'Bbb' in my case. I tried to use this:
$result_array…

Pavel S.
- 11,892
- 18
- 75
- 113
3
votes
2 answers
Preg_split captured all result in array
I have a problem with the function preg_split in php
My regexp is
#^(-?[0-9]+)(([+x])(-?[0-9]+))*$#
I want for example transform :
-5+69x45
in an array like this
{
[0] = -5
[1] = +
[2] = 69
[3] = x
[4] = 45
}
My regexp work and math with…

Jebik
- 778
- 2
- 8
- 26
3
votes
1 answer
PHP preg_split string with spaces and Turkish characters
I'm using preg_split to split the following string:
$string = 'textarea name="custom_field" label="Space space space" column="1/2"';
$preg_split = preg_split("/\s(?![\w\s]+\")/", $string);
echo '
',print_r($preg_split,1),''; This code…

The Bobster
- 573
- 4
- 20
3
votes
1 answer
php preg_split split string to array from square brackets
I want to split the string from square brackets without removing any character. I tried the below code but it's not working for me.
$value = "
Lorem ipsum dolor [State] sit amet, consectetur [City] adipisicing [TopJob7] elit, sed do eiusmod…

Renish Khunt
- 5,620
- 18
- 55
- 92
3
votes
3 answers
preg_split in php to split string at the point which is preceded by digit and is followed by letters or blank space
In order to split my string at the point which is preceded by digit and is followed by letters as:
$str = '12jan';
I have used
$arr = preg_split('/(?<=[0-9])(?=[a-z]+)/i',$str);
It works file and gives the desired output. I want to update it so…

Rekha Rani Paul
- 29
- 3