Questions tagged [preg-split]

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

550 questions
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
  • 283
  • 5
  • 14
5
votes
10 answers

Split a string AFTER every third instance of character

How can I explode a string at the position after every third semicolon (;)? example data: $string = 'piece1;piece2;piece3;piece4;piece5;piece6;piece7;piece8;'; Desired output: $output[0] = 'piece1;piece2:piece3;' $output[1] =…
stunnaman
  • 911
  • 3
  • 13
  • 19
5
votes
5 answers

Split string after every five words

I want to split a string after every five words. Example There is something to type here. This is an example text Output There is something to type here. This is an example text How can this be done using preg_split()? Or is there any way to wrap…
Kamini
  • 393
  • 1
  • 5
  • 16
4
votes
1 answer

PHP add quotes to array for json_decode

I'm looking to json_decode a string, but running into a problem with the array elements not having quotes. JSON {"Status":"DISPUTED","GUID":[]} {"Status":"CONFIRMED","GUID":[G018712, G017623]} PHP $json =…
Jeffrey
  • 4,098
  • 11
  • 42
  • 66
4
votes
3 answers

I am trying to split/explode/preg_split a string but I want to keep the delimiter

I am trying to split/explode/preg_split a string but I want to keep the delimiter example : explode('/block/', '/block/2/page/2/block/3/page/4'); Expected result : array('/block/2/page/2', '/block/3/page/4'); Not sure if I have to loop and then…
Purplefish32
  • 647
  • 1
  • 8
  • 24
4
votes
5 answers

Using Preg_Split With Multiple Spaces

I'm having a bit of trouble figuring this out. I have lines of data such as this: $data = "Alpha Natural Resources Inc COM 02076X102 2,077 45,700 x I am looking to "explode" this line wherever there is more than one…
sysdomatic
  • 41
  • 1
  • 1
  • 2
4
votes
3 answers

How to explode a string by any integer? Regex?

This seems like it must be so simple, but regular expressions are extremely confusing to me. I am grabbing $_GET variables that will look like typeX, optionX, groupX, etc. where X equals any positive whole integer. I need to then split the string at…
Colton McCormack
  • 771
  • 11
  • 23
4
votes
4 answers

Best way to handle splitting a Windows or Linux path

I have two Strings: C:\Users\Bob\My Documents /Users/Bob/Documents I have managed to conjure this regex: preg_split('/(?<=[\/\\\])(?![\/\\\])/', $string) that will return Array ( [0] => C:\ [1] => Users\ [2] => Bob\ [3] => My…
amcc
  • 2,608
  • 1
  • 20
  • 28
4
votes
3 answers

Split string on non-alphanumeric characters and on positions between digits and non-digits

I'm trying to split a string by non-alphanumeric delimiting characters AND between alternations of digits and non-digits. The end result should be a flat array of consisting of alphabetic strings and numeric strings. I'm working in PHP, and would…
mlh
  • 584
  • 2
  • 6
  • 18
4
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
  • 5
  • 20
4
votes
3 answers

Splitting the string based on alphabet followed by numbers

I try to split the string A1B22C333 to get the array [0] => A1 , [1] => B22, [2] => C333. This is my failed attempt: $mystring = "A1B22C333"; $pattern = "[\w\d+]"; $arr = preg_split("/".$pattern."/", $mystring); print_r($arr); But i get: Array (…
Black
  • 18,150
  • 39
  • 158
  • 271
4
votes
2 answers

regexp to split a string using comma(,) delimiter but ignore if the comma is in curly braces{,}

I need a regexp to split a string using comma(,) delimiter but ignore if the comma is in curly braces{,} in the example below; "asd", domain={"id"="test"}, names={"index"="user.all", "show"="user.view"}, test="test" INTO ( IT SHOULD…
Oguzhan
  • 724
  • 6
  • 12
4
votes
5 answers

PHP: How to split a string by dash and everything between brackets. (preg_split or preg_match)

I've been wrapping my head around this for days now, but nothing seems to give the desired result. Example: $var = "Some Words - Other Words (More Words) Dash-Binded-Word"; Desired result: array( [0] => Some Words [1] => Other Words [2] => More…
Sascha
  • 141
  • 1
  • 8
4
votes
1 answer

Splitting a string at question mark, exclamation mark, or period

I'm trying to split a string at a question mark, an exclamation mark, or a period using preg_split, but I've ran into a problem. Instead of splitting at the question mark, it split the string way before that. Please take a look at my code:
jessica
  • 1,667
  • 1
  • 17
  • 35
4
votes
1 answer

Echo the ending part or last folder of $_SERVER['DOCUMENT_ROOT'] in PHP

I need to use $_SERVER['DOCUMENT_ROOT']; to scandir for files and folders to display for my nav menu. Let's say my root directory is at /home/user/public_html/website/. Here's how I echo the root directory: echo $_SERVER['DOCUMENT_ROOT']; it will…
Mike
  • 607
  • 8
  • 30
1 2
3
36 37