Questions tagged [preg-split]

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

531 questions
6
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
  • 69
  • 1
  • 3
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
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
2 answers

preg_split - split by white space and by chosen character but keep the character in array

So i have this problem, i want to split a string by a pattern that contains white space( ) and comma (,). I managed to split my string by that pattern but the problem is that i want to keep that comma in array. Here is my string: $string = "It's…
emma
  • 761
  • 4
  • 16
5
votes
3 answers

PHP preg_split: Split string by forward slash

I want to split my string 192.168.1.1/24 by forward slash using PHP function preg_split. My variable : $ip_address = "192.168.1.1/24"; I have tried : preg_split("/\//", $ip_address); //And preg_split("/[/]/", $ip_address); Error message :…
Zakaria Acharki
  • 63,488
  • 15
  • 64
  • 88
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
  • 19,727
  • 15
  • 78
  • 118
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
  • 175
  • 1
  • 13
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
  • 2
  • 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
  • 7,455
  • 19
  • 51
  • 65
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,467
  • 5
  • 19
  • 30
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
2 answers

Split String into Text and Number

I have some strings which can be in the following format sometext moretext 01 text text sometext moretext 002 text text 1 (somemoretext) etc I want to split these strings into following: text before the number and the number For example: text text…
user1981823
  • 75
  • 1
  • 1
  • 4
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
  • 13,096
  • 38
  • 113
  • 210
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

5
votes
1 answer

Compilation failed: missing terminating ] for character class

$date could be "23/09/2012" or "23-09-2012" or "23\09\2012" preg_split('/[\/\-\\]/', $date); Not sure why PHP keep throw missing terminating ] error?
Xin Chen
  • 163
  • 4
  • 13
1
2
3
35 36