preg-split

preg_split by space and tab outside quotes

旧巷老猫 提交于 2019-12-22 09:55:44
问题 I am trying to get preg_split() to split the following 2 strings, by space/tab (needs to work on both). autodiscover.microsoft.com. 3600 IN A 131.107.125.5 and microsoft.com. 3600 IN TXT "v=spf1 include:_spf-a.microsoft.com include:_spf-b.microsoft.com include:_spf-c.microsoft.com -all" The trick is that in the second instance the last part with quotes, should not be split. From looking on the StackOverflow, I have found that I probably need to use this. $results = preg_split("/'[^']*'(*SKIP)

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

你说的曾经没有我的故事 提交于 2019-12-22 08:17:08
问题 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 tried: $sentence = explode(" ", $sentence); /* returns array(4) { [0]=> string(1) "I" [1]=> string(2) "am" [2]=> string(1) "a" [3]=> string(8) "sentence." } */ And also this: $sentence = preg_split("/[.?!\s]/", $sentence); /* returns array(5) { [0]=> string(1

preg_split unexpected behavior

℡╲_俬逩灬. 提交于 2019-12-20 05:50:49
问题 I use preg_split as the following: <?php $num = 99.14; $pat = "[^a-zA-Z0-9]"; $segments = preg_split($pat, $num); print_r($segments); I expected that $segments will be an array like array(99,14) However, it returns array(99.14) I don't know why preg_split do that while the pattern by which it should split the string is any special character i.e non alphanumeric. Check this demo: http://codepad.org/MUusnwis 回答1: You have to add delimiters to your regex like this: <?php $num = 99.14; $pat = "/[

PHP REGEX - text to array by preg_split at line break

寵の児 提交于 2019-12-19 09:28:03
问题 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 normal text ) ok, now by using preg_split( '#\n(?!s)#' , $text ); i get [0] => Array ( [0] => some normal text [1] => some long text here, and so on... sometimes [2] => some normal text [3] => some normal text ) I want get this: [0] => Array ( [0] => some normal text [1] => some long text here, and so on... sometimes i'm

How to explode a string by any integer? Regex?

蓝咒 提交于 2019-12-19 08:45:10
问题 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+#",

How to explode a string by any integer? Regex?

老子叫甜甜 提交于 2019-12-19 08:45:08
问题 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+#",

PHP split to preg_split()

笑着哭i 提交于 2019-12-19 05:45:19
问题 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 = 'D:/Projects/job.com/www/www/path/source'; $array = split('www', 'D:/Projects/job.com/www/www/path/source', 2); print_r($array); Output of the split function: Array ( [0] => D:/Projects/job.com/ [1] => /www/path/source ) 回答1: preg_split() is similar to the old ereg-function split().

Compilation failed: missing terminating ] for character class

放肆的年华 提交于 2019-12-18 17:14:25
问题 $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 ? 回答1: 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]

Regex to split string into array of numbers and characters using PHP

拟墨画扇 提交于 2019-12-13 11:18:12
问题 I have an arithmetic string that will be similar to the following pattern. a. 1+2+3 b. 2/1*100 c. 1+2+3/3*100 d. (1*2)/(3*4)*100 Points to note are that 1. the string will never contain spaces. 2. the string will always be a combination of Numbers, Arithmetic symbols (+, -, *, /) and the characters '(' and ')' I am looking for a regex in PHP to split the characters based on their type and form an array of individual string characters like below. (Note: I cannot use str_split because I want

Making a [code][/code] for BBcode with php regex

一曲冷凌霜 提交于 2019-12-12 10:03:42
问题 I would like to make a [code][/code] tag for bbcode so that what would be inside wouldn't be taken into account by the php regex that I made. Example : Hello [b]newbie[/b], to write in bold, use the following : [code][b](YOURTEXT)[/b][/code] Should return in HTML : Hello <strong>newbie</strong>, to write in bold, use the following : [b](YOURTEXT)[/b] Here is a view of a part of my bbcode function : <? function bbcode($var) { $var = preg_replace('`\[b\](.+)\[/b\]`isU', '<strong>$1</strong>',