Questions tagged [preg-split]

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

550 questions
2
votes
2 answers

preg_split() String into Text and Numbers

i want to split the string into numbers and text as separate. print_r(preg_split("/[^0-9]+/", "12345hello")); output: Array ( [0] => 12345 [1] => )
Ram
  • 506
  • 2
  • 5
  • 15
2
votes
1 answer

PHP tokenize a tweet in words, punctuation, hashtag, mentions, emoticons

I would like to tokenize a tweet. As you probably know, tweets usually have informal forms, as follow: This is a common Tweet #format where @mentions and.errors!!!!like this:-))))) might #appear❤ ❤☺❤#ThisIsAHashtag!?! You may also have emoji in…
Mauro
  • 361
  • 1
  • 4
  • 14
2
votes
3 answers

Double forward slash in regular expression

I want to use preg_split to split a string on double forward slash (//) and question mark (?). I can use a single forward slash like this: preg_split('[\/]', $string); But preg_split('[\//]', $string); or preg_split('[\/\/]', $string); do not…
Lennart
  • 1,018
  • 1
  • 12
  • 27
2
votes
3 answers

How to capture delimiter in different array or designated key with preg_split?

Update: first question resolved, was an issue with HTML/browser. Second question: Is there a way to output the delimiters into a separate array? If I use PREG_SPLIT_DELIM_CAPTURE, it mixes the delimiters into the same array and makes it confusing…
Devon Bessemer
  • 34,461
  • 9
  • 69
  • 95
2
votes
1 answer

Remove empty items from preg_split result array

I am trying to clear a string of " " , , , . , ? , ! but the result array contains an empty element. My code: $mesaj = "Ana are mere, dar nu are si nuci? Matei are nuci!"; $keywords = preg_split("/[\s,.?!]+/", $mesaj); print_r($keywords); The…
Filip
  • 147
  • 9
2
votes
2 answers

php preg_split first uppercase

Using the following code, php explode at capital letters? you can explode the string by uppercase. But how do you explode it only on the first uppercase? Say you have helloThereMister. I want to get hello ThereMister. I can concatenate the result…
Kousha
  • 32,871
  • 51
  • 172
  • 296
2
votes
3 answers

Attempting to preg_split a large, multiline file into an array

I have a file formatted as... file.txt [sectionone] ... ... [sectiontwo] ... ... [sectionthree] ... ... The format is very similar to (for those familiar) smb.conf and I was hoping to have an array "section" strings by the end of it. In the end I'm…
Evan
  • 63
  • 5
2
votes
2 answers

How can I explode a string by more than one space, but not by exactly one space in php

I have a text line that is delimited by tabs and multiple spaces but not single spaces. I have found: parsed = preg_split('/ +/', $line); // one or more spaces which can a split a line with one or more spaces. Is there a regex for only more than…
user1592380
  • 34,265
  • 92
  • 284
  • 515
2
votes
2 answers

How to match commas and "and" with preg_split in PHP

I have this string: Rosemary J. Harris $^{1}$, Vladislav Popkov $^{2}$ and Gunter M. Sch\"utz $^{3,}$* And I would liek to split it with commas (,) or "and" string. So, the result should be: [0] -> Rosemary J. Harris $^{1}$ [1] -> Vladislav Popkov…
Milos Cuculovic
  • 19,631
  • 51
  • 159
  • 265
2
votes
1 answer

PHP regular expression syntax for word phrase?

I am new to PHP regular expression syntax. Using the following two lines, I was able to phrase a website depending on the following characters : ’ newline ” $html = @file_get_contents("http://news.yahoo.com"); $webwords = preg_split("/[:'\n\"]+/",…
D P.
  • 1,039
  • 7
  • 27
  • 56
2
votes
3 answers

PHP preg_replace, split or match?

I need to parse a string and replace a specific format for tv show names that don't fit my normal format of my media player's queue. Some examples Show.Name.2x01.HDTV.x264 should be Show.Name.S02E01.HDTV.x264 Show.Name.10x05.HDTV.XviD should be…
2
votes
5 answers

Splitting up html code tags and content

Does anyone with more knowledge than me about regular expressions know how to split up html code so that all tags and all words are seperated ie.

Some content A link

Is seperated like this: array = { [0]=>"

", …

FlimFlam
  • 483
  • 1
  • 7
  • 13
2
votes
4 answers

PHP Regex correct syntax preg_split after any number of words and 1-3 digits

I'm trying to break down a rss feed with sports scores Example data San Diego 4 Chicago Cubs 2 Miami 2 Philadelphia 7 Boston 3 Toronto 1 Washington 3 Atlanta 1 Chicago Sox 3 Texas 1 St. Louis 6 Milwaukee 5 The rss basically gives me one flowing…
2
votes
1 answer

Regular expression to split a string except in URLs

I need to split a string on any non-alphanumeric character except / and -. For example, in preg_split(): /[^a-zA-Z0-9\/\-]/ This works great, but now I want to split the string at all these points except when the characters are found in a URL (i.e.…
Matt
  • 22,721
  • 17
  • 71
  • 112
2
votes
1 answer

Explode string after price

Say I have a string $what = "1 x Book @ $20 32 x rock music cd @ $400 1 x shipping to india @ $10.5"; And I want to explode so that output is Array ( [0] => 1 x Book @ $20 [1] => 32 x rock music cd @ $400 [2] => 1 x shipping to india…
pessato
  • 501
  • 6
  • 17