Questions tagged [preg-split]

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

550 questions
217
votes
11 answers

Explode string on commas and trim potential spaces from each value

For example, I would like to create an array from the elements in this string: $str = 'red, green, blue ,orange'; I know you can explode and loop through them and trim: $arr = explode(',', $str); foreach ($arr as $value) { $new_arr[] =…
SeanWM
  • 16,789
  • 7
  • 51
  • 83
52
votes
5 answers

Split string by delimiter, but not if it is escaped

How can I split a string by a delimiter, but not if it is escaped? For example, I have a string: 1|2\|2|3\\|4\\\|4 The delimiter is | and an escaped delimiter is \|. Furthermore I want to ignore escaped backslashes, so in \\| the | would still be a…
Anton
  • 811
  • 9
  • 13
30
votes
2 answers

How do I include the split delimiter in results for preg_split()?

I have this simple pattern that splits a text into periods: $text = preg_split("/[\.:!\?]+/", $text); But I want to include . : or ! at the end of the array items. That is, now for "good:news.everyone!" I have: array("good", "news", "everyone",…
skyline26
  • 1,994
  • 4
  • 23
  • 35
25
votes
3 answers

In PHP, which is faster: preg_split or explode?

Which is faster when using it to extract keywords in a search query in php: $keyword = preg_split('/[\s]+/', $_GET['search']); or $keyword = explode(' ', $_GET['search']);
Marco
  • 2,687
  • 7
  • 45
  • 61
18
votes
5 answers

Match all substrings that end with 4 digits using regular expressions

I am trying to split a string in php, which looks like this: ABCDE1234ABCD1234ABCDEF1234 Into an array of string which, in this case, would look like this: ABCDE1234 ABCD1234 ABCDEF1234 So the pattern is "an undefined number of letters, and then 4…
DevBob
  • 835
  • 2
  • 9
  • 29
15
votes
3 answers

Split a text by a backslash \ ?

I've searched for hours. How can I separate a string by a "\" I need to separate HORSE\COW into two words and lose the backslash.
user723220
  • 817
  • 3
  • 12
  • 20
12
votes
3 answers

Split at capital letters in PHP?

I found this similar question, but it doesn't answer my question, Split word by capital letter. I have a string which is camel case. I want to break up that string at each capital letter like below. $str = 'CamelCase'; // array('Camel', 'Case'); I…
ShaShads
  • 582
  • 4
  • 12
10
votes
5 answers

Regular expression for preg_split() by new line

This is my file: 0.0 5.0 5.0 6.0 7.0 2.0 5.0 2.0 1.0 5.0 5.0 1.0 2.0 7.1 5.0 5.0 0.0 5.0 5.0 5.0 2.0 5.0 1.0 5.0 6.0 6.0 6.0 6.0 1.0 7.1 5.0 5.0 0.0 6.0 1.0 6.0 5.0 5.0 1.0 6.0 5.0 7.0 1.0 5.0 6.0 6.0 5.0 6.0 0.0 5.0 2.0 1.0 6.0 5.0 6.0 2.0 1.0 2.0…
michele
  • 26,348
  • 30
  • 111
  • 168
10
votes
4 answers

how to use preg_split() in php?

Can anybody explain to me how to use preg_split() function? I didn't understand the pattern parameter like this "/[\s,]+/". for example: I have this subject: is is. and I want the results to be: array ( 0 => 'is', 1 => 'is', ) so it will…
MD.MD
  • 708
  • 4
  • 14
  • 34
10
votes
2 answers

Split a text into sentences

How can I split a text into an array of sentences? Example text: Fry me a Beaver. Fry me a Beaver! Fry me a Beaver? Fry me Beaver no. 4?! Fry me many Beavers... End Should output: 0 => Fry me a Beaver. 1 => Fry me a Beaver! 2 => Fry me a…
thelolcat
  • 10,995
  • 21
  • 60
  • 102
9
votes
5 answers

how to remove multiple slashes in URI with 'PREG' or 'HTACCESS'

how to remove multiple slashes in URI with 'PREG' or 'HTACCESS' site.com/edition/new/// -> site.com/edition/new/ site.com/edition///new/ -> site.com/edition/new/ thanks
Lelis
  • 325
  • 1
  • 3
  • 11
9
votes
1 answer

PHP split to preg_split()

I wanted to convert the following split function, which I have been using to preg_split.. it's a little confusing, because the value will change from time to time... Current code: $root_dir = 'www'; $current_dir =…
Basit
  • 16,316
  • 31
  • 93
  • 154
8
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
  • 66,747
  • 15
  • 75
  • 101
8
votes
1 answer

Function split() is deprecated , preg_split(): No ending delimiter ',' found

I have a PHP script written 10 years ago. Now we moved the script to new server and it's not working. The line that has problem is: $p_industry = split(',', $member['p_industry']); The testing email receive this error message: Function split() is…
happybeauty
  • 81
  • 1
  • 4
8
votes
3 answers

How to split a string on comma that is NOT followed by a space?

I want the results to be: Cats, Felines & Cougars Dogs Snakes This is the closest I can get. $string = "Cats, Felines & Cougars,Dogs,Snakes"; $result = split(',[^ ]', $string); print_r($result); Which results in Array ( [0] => Cats,…
Jeremy Gehrs
  • 413
  • 6
  • 17
1
2 3
36 37