Questions tagged [preg-split]

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

531 questions
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
  • 3,888
  • 10
  • 40
  • 63
4
votes
2 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
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
  • 11,936
  • 14
  • 66
  • 125
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…
Colt McCormack
  • 761
  • 11
  • 22
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,459
  • 1
  • 17
  • 27
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
  • 12,789
  • 26
  • 116
  • 196
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

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
  • 609
  • 5
  • 25
4
votes
5 answers

PHP REGEX - text to array by preg_split at line break

EDITED: need help on split Array array example: array ( [0] => :some normal text :some long text here, and so on... sometimes i'm breaking down and... :some normal text :some…
Luca Filosofi
  • 30,086
  • 8
  • 65
  • 74
4
votes
1 answer

Combine regular expressions for splitting camelCase string into words

I managed to implement a function that converts camel case to words, by using the solution suggested by @ridgerunner in this question: Split camelCase word into words with php preg_match (Regular Expression) However, I want to also handle embedded…
stou
  • 63
  • 6
4
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,143
  • 15
  • 64
  • 130
4
votes
3 answers

How can I split a sentence into words and punctuation marks?

For example, I want to split this sentence: I am a sentence. Into an array with 5 parts; I, am, a, sentence, and .. I'm currently using preg_split after trying explode, but I can't seem to find something suitable. This is what I've…
Lucas
  • 15,579
  • 27
  • 98
  • 170
4
votes
1 answer

PHP Regex to match the last occurrence of a string

My string is $text1 = 'A373R12345' I want to find last none digital number occurrence of this string. So I use this regular expression ^(.*)[^0-9]([^-]*) Then I got this result: 1.A373 2.12345 But my expected result is: 1.A373R (It has…
Nick Hung
  • 43
  • 1
  • 6
4
votes
1 answer

preg_split with two patterns (one of them quoted)

I would like to split a string in PHP containing quoted and unquoted substrings. Let's say I have the following string: "this is a string" cat dog "cow" The splitted array should look like this: array ( [0] => "this is a string" [1] => "cat" …
Stefan
  • 317
  • 5
  • 19
1 2
3
35 36