preg_split() is a PHP function which allows splitting a string using a regular expression.
Questions tagged [preg-split]
531 questions
52
votes
5 answers
Split string by delimiter, but not if it is escaped
How can I split a string by a delimiter, but not if it is escaped? For example, I have a string:
1|2\|2|3\\|4\\\|4
The delimiter is | and an escaped delimiter is \|. Furthermore I want to ignore escaped backslashes, so in \\| the | would still be a…
![](../../users/profiles/784815.webp)
Anton
- 791
- 7
- 13
23
votes
2 answers
How do I include the split delimiter in results for preg_split()?
I have this simple pattern that splits a text into periods:
$text = preg_split("/[\.:!\?]+/", $text);
But I want to include . : or ! at the end of the array items.
That is, now for "good:news.everyone!" I have:
array("good", "news", "everyone",…
![](../../users/profiles/1252794.webp)
skyline26
- 1,834
- 4
- 20
- 34
22
votes
3 answers
In PHP, which is faster: preg_split or explode?
This may sound like a stupid question, but: which is faster when using it to extract keywords in a search query in php:
$keyword = preg_split('/[\s]+/', $_GET['search']);
or
$keyword = explode(' ', $_GET['search']);
![](../../users/profiles/888139.webp)
Marco
- 2,435
- 4
- 35
- 56
18
votes
5 answers
Match all substrings that end with 4 digits using regular expressions
I am trying to split a string in php, which looks like this:
ABCDE1234ABCD1234ABCDEF1234
Into an array of string which, in this case, would look like this:
ABCDE1234
ABCD1234
ABCDEF1234
So the pattern is "an undefined number of letters, and then 4…
![](../../users/profiles/1372393.webp)
DevBob
- 795
- 1
- 6
- 27
13
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.
![](../../users/profiles/723220.webp)
user723220
- 707
- 3
- 10
- 19
10
votes
4 answers
Regular expression for preg_split() by new line
This is my file:
0.0 5.0 5.0 6.0 7.0 2.0 5.0 2.0 1.0 5.0 5.0 1.0 2.0 7.1 5.0
5.0 0.0 5.0 5.0 5.0 2.0 5.0 1.0 5.0 6.0 6.0 6.0 6.0 1.0 7.1
5.0 5.0 0.0 6.0 1.0 6.0 5.0 5.0 1.0 6.0 5.0 7.0 1.0 5.0 6.0
6.0 5.0 6.0 0.0 5.0 2.0 1.0 6.0 5.0 6.0 2.0 1.0 2.0…
![](../../users/profiles/453712.webp)
michele
- 24,748
- 27
- 92
- 146
10
votes
4 answers
how to use preg_split() in php?
Can anybody explain to me how to use preg_split() function?
I didn't understand the pattern parameter like this "/[\s,]+/".
for example:
I have this subject: is is. and I want the results to be:
array (
0 => 'is',
1 => 'is',
)
so it will…
![](../../users/profiles/3194430.webp)
MD.MD
- 720
- 3
- 13
- 32
10
votes
3 answers
Split at capital letters in PHP?
I found this similar question, but it doesn't answer my question, Split word by capital letter.
I have a string which is camel case. I want to break up that string at each capital letter like below.
$str = 'CamelCase'; // array('Camel', 'Case');
I…
![](../../users/profiles/2094173.webp)
ShaShads
- 552
- 4
- 12
9
votes
5 answers
how to remove multiple slashes in URI with 'PREG' or 'HTACCESS'
how to remove multiple slashes in URI with 'PREG' or 'HTACCESS'
site.com/edition/new/// -> site.com/edition/new/
site.com/edition///new/ -> site.com/edition/new/
thanks
![](../../users/profiles/769082.webp)
Lelis
- 325
- 1
- 3
- 11
9
votes
1 answer
PHP split to preg_split()
I wanted to convert the following split function, which I have been using to preg_split.. it's a little confusing, because the value will change from time to time...
Current code:
$root_dir = 'www';
$current_dir =…
![](../../users/profiles/75799.webp)
Basit
- 13,920
- 29
- 88
- 145
9
votes
1 answer
Split a text into sentences
How can I split a text into an array of sentences?
Example text:
Fry me a Beaver. Fry me a Beaver! Fry me a Beaver?
Fry me Beaver no. 4?! Fry me many Beavers... End
Should output:
0 => Fry me a Beaver.
1 => Fry me a Beaver!
2 => Fry me a…
![](../../users/profiles/1209030.webp)
thelolcat
- 9,215
- 18
- 56
- 94
8
votes
1 answer
Function split() is deprecated , preg_split(): No ending delimiter ',' found
I have a PHP script written 10 years ago. Now we moved the script to new server and it's not working. The line that has problem is:
$p_industry = split(',', $member['p_industry']);
The testing email receive this error message:
Function split() is…
![](../../users/profiles/4915318.webp)
happybeauty
- 81
- 1
- 4
8
votes
3 answers
How to split a string on comma that is NOT followed by a space?
I want the results to be:
Cats, Felines & Cougars
Dogs
Snakes
This is the closest I can get.
$string = "Cats, Felines & Cougars,Dogs,Snakes";
$result = split(',[^ ]', $string);
print_r($result);
Which results in
Array
(
[0] => Cats,…
![](../../users/profiles/890481.webp)
Jeremy Gehrs
- 413
- 6
- 17
6
votes
3 answers
How to split text to match double quotes plus trailing text to dot?
How can I get a sentence that is in double quotes in which there is a dot that must be split?
Example document like this:
“Chess helps us overcome difficulties and sufferings,” said Unnikrishnan, taking my queen. “On a chess board you are fighting.…
![](../../users/profiles/8039518.webp)
Rachmad
- 141
- 8
6
votes
2 answers
use preg_split to split chords and words
I'm working on a little piece of code playing handling song tabs, but i'm stuck on a problem.
I need to parse each song tab line and to split it to get chunks of chords on the one hand, and words in the other.
Each chunk would be like :
$line_chunk…
![](../../users/profiles/782013.webp)
gordie
- 1,035
- 2
- 12
- 25