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
PHP preg_split regex does not split with dot
I'm using the following regex to split a string to an array. Everything goes fine but for some reason it does not the splitting with \.(space). How would i need to change it to make it work?
$sentences = preg_split(" / (\. |, and|, or|, but|,…

bicycle
- 8,315
- 9
- 52
- 72
3
votes
4 answers
string to array, split by single and double quotes
i'm trying to use php to split a string into array components using either " or ' as the delimiter. i just want to split by the outermost string. here are four examples and the desired result for each:
$pattern = "?????";
$str = "the cat 'sat on'…

mulllhausen
- 4,225
- 7
- 49
- 71
2
votes
3 answers
PHP preg_split() not capturing the split in the string
I'm trying to use a regex with preg_split to separate a url from a string:
$body = "blah blah blah http://localhost/tomato/veggie?=32";
$regex = "(((f|ht){1}tp://)[-a-zA-Z0-9@:%_\+.~#?&//=]+)";
$url = preg_split($regex, $body);
The…

Akersh
- 43
- 4
2
votes
2 answers
Please explain this regex to me
I came across the following to split a string into "tokens":
$tokens = preg_split("/[^\-_A-Za-z0-9]+/", $string);
Could somebody explain to me how this is different from this:
$tokens = explode(' ', $string);
Any help would be greatly appreciated…

Pr0no
- 3,910
- 21
- 74
- 121
2
votes
3 answers
preg_split string into tokens
I'm trying to write a regex with which I can split a string into tokens. This used to work:
$rawtokens = split("[^-_A-Za-z0-9]+", $string);
But now split() has been deprecated (and using preg_split is recommended), this doesn't work:
$rawtokens =…

Pr0no
- 3,910
- 21
- 74
- 121
2
votes
3 answers
How to group sentences by groups of 500 characters without breaking a sentence with PHP?
I have been scratching my head on this but cannot work out a solution.
Let's say you have a text of 5000 characters, I would like to split it into blocks of less than 500 characters, but, without breaking a single sentence. eg: if a paragraph is…

Benny
- 430
- 6
- 17
2
votes
3 answers
php custom parse a string with preg
My methods parse strings and do stuff with the information contained in the string.
a simple example would be
$string=
"
user:name,password={$_POST['name']}, {$_POST['password']}(md5);
names:name=name;
";
$class->$method($string);
The…

Yamiko
- 5,303
- 5
- 30
- 52
2
votes
2 answers
How to exclude string from preg_split when string already contains splittable character in PHP?
I am using the preg_split function in PHP in order to create one array containing several different elements. However, I want to exclude a string which happens to contain one of the elements that I'm preg_splitting by.
$array['stuff'] =…

Tony
- 23
- 5
2
votes
4 answers
Split lines of space-delimited values to get flat array of 2nd and 3rd column values
I have the following data which is displaying as this
{123456 123456 123456}
{654321 654321 654321}
{123456 123456 123456}
My PHP Code:
$myarray = preg_split("/(\s|{\s)/", $data);
print_r($myarray);
The output of my array is like this:
[0]…

thebest man
- 109
- 5
2
votes
1 answer
arabic characters to ABCD characters
I am working with below code in php to show the arabic letters in roman ABCD characters as defined below in my code.
But it is not displaying properly. It is losing the character sorting also and not displaying some of the characters according to my…

usman610
- 479
- 1
- 5
- 22
2
votes
2 answers
Split mathematic expression into array without splitting subexpressions between parentheses and single quotes
Let's say I have this string:
1 + 2 * (3 + (23 + 53 - (132 / 5) + 5) - 1) + 2 / 'test + string' - 52
I want to split it into an array of operators and non-operators, but anything between the () and ' must not be split.
I want the output to be:
[1,…

Saif Alhammouri
- 35
- 7
2
votes
3 answers
How to Split/Explode a string into a numbered lists in PHP?
I am trying to split this string into numbered lists..
str options is "Hello,Howdy,Hola".
I need the output to be
Hello
Howdy
Hola
This is my code
$newstr = 'My values are' . explode(",", $str["options"]) . '
'; However, This causes to only…
'; However, This causes to only…

user6345655
- 57
- 4
2
votes
2 answers
Break a string into parts, returning all characters
I want to break a string according to the following rules:
all consecutive alpha-numeric chars, plus the dot (.) must be treated as one part
all other consecutive chars must be treated as one part
consecutive combinations of 1 and 2 must be treated…

BenMorel
- 34,448
- 50
- 182
- 322
2
votes
3 answers
How to split string by slash which is not between numbers?
How to split string by slash which is not between numbers?
I am using preg_split function below:
$splitted = preg_split('#[/\\\\\_\s]+#u', $string);
Input: "925/123 Black/Jack"
Splitted result now:
[
0 => '925',
1 => '123',
2 =>…

Jackson
- 23
- 3
2
votes
1 answer
Is there any option to separate special characters from words with regex/preg_split?
I'm junior and not at ease with regex, and I'm trying to do a password generator with sentences using regex and preg_split.
All is done except one thing, for example the sentence "I've got 2 cats." should result as "I'vg2c." but the only thing I…