preg-split

string to array, split by single and double quotes

人走茶凉 提交于 2019-12-01 20:52:52
i'm trying to use php to split a string into array components using either " or ' as the delimiter. i just want to split by the outermost string. here are four examples and the desired result for each: $pattern = "?????"; $str = "the cat 'sat on' the mat"; $res = preg_split($pattern, $str); print_r($res); /*output: Array ( [0] => the cat [1] => 'sat on' [2] => the mat )*/ $str = "the cat \"sat on\" the mat"; $res = preg_split($pattern, $str); print_r($res); /*output: Array ( [0] => the cat [1] => "sat on" [2] => the mat )*/ $str = "the \"cat 'sat' on\" the mat"; $res = preg_split($pattern,

How to explode a string by any integer? Regex?

故事扮演 提交于 2019-12-01 08:30:01
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 the integer so that type1 becomes an array of "type and "1", however type10 must become an array of "type and "10" not "type" and "1" and "0". I am wholly unfamiliar with regex patterns but ended up coming up with: $array = preg_split("#\\d+#", $key, -1, PREG_SPLIT_NO_EMPTY); print_r($array); but it just removed the number from the end rather than

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

天涯浪子 提交于 2019-11-30 20:17:01
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 using the plus symbol + in regex means the occurrence of one or more of the previous character. So we can add it in a preg_replace to replace the occurrence of one or more / by just one of them $url = "site.com/edition/new///"; $newUrl = preg_replace('/(\/+)/','/',$url); // now it should be replace with the correct single forward slash echo $newUrl $url = 'http://www.abc.com/def/git//ss'; $url = preg_replace('/([^:])(\/{2,})/',

Compilation failed: missing terminating ] for character class

风流意气都作罢 提交于 2019-11-30 14:43:43
$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 ? preg_split('/[\/\-\\]/', $date); ^escaping the closing ']' Do the following instead, to remove ambiguity preg_split('/[\/\-\\\\]/', $date); There is no need to escape - , but you could use \- as well. Code: $date = 'as\sad-s/p'; $slices = preg_split('/[\/\-\\\\]/', $date); print_r($slices); Output: Array ( [0] => as [1] => sad [2] => s [3] => p ) 来源: https://stackoverflow.com/questions/13173890/compilation-failed-missing-terminating-for

PHP preg_split while keeping delimiter at the start of array element

给你一囗甜甜゛ 提交于 2019-11-30 09:47:56
问题 I'm new to regex, and I want to split a string on a delimiter, while keeping that delimiter at the beginning of each array element. I tried it unsuccessfully, and came up with: $str = 'LOTS OF STUFF AND SOME MORE STUFF AND SOME OTHER STUFF'; $matches = preg_split('/(\ AND)/', $str, null, PREG_SPLIT_DELIM_CAPTURE); This puts the delimiter as its own element : [0]=>"LOTS OF STUFF" [1]=>" AND" [2]=>" SOME MORE STUFF" [3]=>" AND" [4]=>" SOME OTHER STUFF" But I wanted to keep the delimiter at the

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

a 夏天 提交于 2019-11-30 03:32:28
问题 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 回答1: using the plus symbol + in regex means the occurrence of one or more of the previous character. So we can add it in a preg_replace to replace the occurrence of one or more / by just one of them $url = "site.com/edition/new///"; $newUrl = preg_replace('/(\/+)/','/',$url); // now it should be replace with the correct single

Split a text into sentences

♀尐吖头ヾ 提交于 2019-11-28 10:09:36
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 Beaver? 3 => Fry me Beaver no. 4?! 4 => Fry me many Beavers... 5 => End I tried some solutions that I've found on SO through search, but they all fail, especially at the 4th sentence. /(?<=[!?.])./ /\.|\?|!/ /((?<=[a-z0-9)][.?!])|(?<=[a-z0-9][.?!]\"))(\s|\r\n)(?=\"?[A-Z])/ /(?<=[.!?]|[.!?][\'"])\s+/ // <- closest one Since you want to "split" sentences why are

Split String into Text and Number

无人久伴 提交于 2019-11-27 15:46:36
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 1 (somemoretext) When split will output: text = text text number = 1 Anything after the number can be discarded Have read up about using regular expressions and maybe using preg_match or preg_split but am lost when it comes to the regular expression part Joseph Silber preg_match('/[^\d]+/', $string, $textMatch); preg_match('/\d+/', $string, $numMatch

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

泪湿孤枕 提交于 2019-11-27 08:19:26
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", ""); But I want: array("good:", "news.", "everyone!", ""); Here you go: preg_split('/([^.:!?]+[.:!?]+)/', 'good:news.everyone!', -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY); How it works: The pattern actually turns everything into a delimiter. Then, to include these delimiters in the array, you can use the PREG_SPLIT_DELIM_CAPTURE constant. This

Split a text into sentences

强颜欢笑 提交于 2019-11-27 03:26:57
问题 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 Beaver? 3 => Fry me Beaver no. 4?! 4 => Fry me many Beavers... 5 => End I tried some solutions that I've found on SO through search, but they all fail, especially at the 4th sentence. /(?<=[!?.])./ /\.|\?|!/ /((?<=[a-z0-9)][.?!])|(?<=[a-z0-9][.?!]\"))(\s|\r\n