Questions tagged [preg-split]

preg_split() is a PHP function which allows splitting a string using a regular expression.

550 questions
7
votes
2 answers

PHP: How to split a UTF-8 string?

I have the following code which is not working with UTF-8 characters. How can I fix it? $seed = preg_split('//u', $seed, -1, PREG_SPLIT_NO_EMPTY); $seed = str_split('АБВГДЕЖЗ'); // and any other characters shuffle($seed); // probably optional since…
user3288430
  • 79
  • 1
  • 3
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.…
Rachmad
  • 149
  • 10
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…
gordie
  • 1,637
  • 3
  • 21
  • 41
6
votes
4 answers

Explode string only once on first occurring substring

preg_match() gives one match. preg_match_all() returns all matches. preg_split() returns all splits. How can I split only on the first match? Example: preg_split("~\n~", $string); This is what I want: array( 0 => 'first piece', 1 => 'the rest…
albus
  • 61
  • 1
  • 3
5
votes
4 answers

PHP preg_split: Split string by other strings

I want to split a large string by a series of words. E.g. $splitby = array('these','are','the','words','to','split','by'); $text = 'This is the string which needs to be split by the above words.'; Then the results would be: $text[0]='This…
Alasdair
  • 13,348
  • 18
  • 82
  • 138
5
votes
2 answers

Regex to match comma not between grouping symbols

I need a regular expression that will match a comma that is NOT between either a '[' and ']' or '(' and ')' or '{' and '}'. Other grouping symbols do not matter. I have tried to figure it out but I cannot come up with anything that accomplishes…
GotCake
  • 51
  • 3
5
votes
3 answers

split string by spaces and colon but not if inside quotes

having a string like this: $str = "dateto:'2015-10-07 15:05' xxxx datefrom:'2015-10-09 15:05' yyyy asdf" the desired result is: [0] => Array ( [0] => dateto:'2015-10-07 15:05' [1] => xxxx [2] => datefrom:'2015-10-09 15:05' [3] =>…
caramba
  • 21,963
  • 19
  • 86
  • 127
5
votes
2 answers

Split the first space and the last space in a string and limit the size of output in 3

Suppose I have a string: ABC DEF SASF 123 (35) And my expected output like: Array ( [0] => ABC [1] => DEF SASF 123 [2] => (35) ) I am trying to do this with $str = preg_split("/(^\S+\s+)|\s+(?=\S+$)/",$str, 3); But the current…
user3571945
  • 184
  • 1
  • 14
5
votes
3 answers

Regex (preg_split): how do I split based on a delimiter, excluding delimiters included in a pair of quotes?

I split this: 1 2 3 4/5/6 "7/8 9" 10 into this: 1 2 3 4 5 6 "7/8 9" 10 with preg_split() So my question is, how do I split based on a delimiter, excluding delimiters inside a pair of quotes? I kind of want to avoid capturing the things in quotes…
theamycode
  • 219
  • 3
  • 10
5
votes
1 answer

php preg_split last occurrence of character

Looking for some help! I need to split a string at the last occurrence of a space... e.g. "Great Neck NY" I need to split it so I have "Great Neck" and "NY" I haven't had a problem using preg_split with basic stuff but I'm stumped trying to…
mike
  • 8,041
  • 19
  • 53
  • 68
5
votes
3 answers

php preg_split error when switching from split to preg_split

I get this warning from php after the change from split to preg_split for php 5.3 compatibility : PHP Warning: preg_split(): Delimiter must not be alphanumeric or backslash the php code is : $statements = preg_split("\\s*;\\s*", $content); How…
benjisail
  • 1,646
  • 5
  • 21
  • 35
5
votes
3 answers

php: split string until first occurance of a number

i have string like cream 100G sup 5mg Children i want to split it before the first occurrence of a digit. so the result should be array( array('cream','100G'), array('sup','5mg Children') ); can so one tell me how to create pattern for…
Zalaboza
  • 8,899
  • 16
  • 77
  • 142
5
votes
2 answers

PHP preg_split utf8 characters

Have problem with preg split and utf. This is code: $original['words'] = preg_split("/[\s]+/", $original['text']); print_r($original); This is answer: Array ( [text] => Šios baterijos kaista [words] => Array ( [0] => � …
Paulius
  • 99
  • 1
  • 7
5
votes
3 answers

preg_split with two delimiters in PHP

How to merge two delimiters in preg_split? For example: $str = "this is a test , and more"; $array = preg_split('/( |,)/', $str, -1, PREG_SPLIT_DELIM_CAPTURE); print_r($array); will produce an array as Array ( [0] => this [1] => [2]…
Googlebot
  • 15,159
  • 44
  • 133
  • 229
5
votes
1 answer

preg_split mixed HTML and PHP tags except in quotes and comments

I have a php page mixed with HTML. Some example code: some text

"; ?>/* */

some HTML text

1
2
3
36 37