preg-split

PHP mb_split(), capturing delimiters

不打扰是莪最后的温柔 提交于 2021-02-09 11:55:41
问题 preg_split has an optional PREG_SPLIT_DELIM_CAPTURE flag, which also returns all delimiters in the returned array. mb_split does not. Is there any way to split a multibyte string (not just UTF-8, but all kinds) and capture the delimiters? I'm trying to make a multibyte-safe linebreak splitter, keeping the linebreaks, but would prefer a more genericaly usable solution. Solution Thanks to user Casimir et Hippolyte, I built a solution and posted it on github (https://github.com/vanderlee/PHP

Split a text by a backslash \ ?

人走茶凉 提交于 2021-02-04 14:42:19
问题 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. 回答1: $array = explode("\\",$string); This will give you an array, for "HORSE\COW" it will give $array[0] = "HORSE" and $array[1] = "COW" . With "HORSE\COW\CHICKEN" , $array[2] would be "CHICKEN" Since backslashes are the escape character, they must be escaped by another backslash. 回答2: You would use explode() and escape the escape character ( \ ). $str = 'HORSE\COW

Split PHP Variable in to array

帅比萌擦擦* 提交于 2021-01-29 11:20:41
问题 A php variable contains $string = "256 Engineering Maths-I 21 -1 21 F"; printing $string gives output 256 Engineering Maths-I 21 -1 21 F this variable should split in to $n[0] = "256"; $n[1] = "Engineering Maths-I"; $n[2] = "21"; $n[3] = "-1"; $n[4] = "21"; $n[5] = "F"; I have tried with $n = explode(" ", $string); but it is splitting in to 2 parts Please help me 回答1: What you are probably looking at is a tab separated string Do this $n = explode("\t", $string); UPDATE The answer was that the

PHP preg_split get rid of trailing spaces in just one line?

こ雲淡風輕ζ 提交于 2021-01-29 03:09:29
问题 How can i get rid of trailing spaces in preg_split result without using preg_replace to first remove all spaces from $test string? $test = 'One , Two, Thee '; $test = preg_replace('/\s+/', ' ', $test); $pieces = preg_split("/[,]/", $test); 回答1: If it must be preg_split() (you actually required that in the question) then this might help: $test = 'One , Two, Thee '; $pieces = preg_split("/\s*,\s*/", trim($test), -1, PREG_SPLIT_NO_EMPTY); trim() is used to remove space before the first and

PHP split string on word before colon

[亡魂溺海] 提交于 2020-07-31 06:01:25
问题 I have a string that looks like this: aaaaa: lorem ipsum bb: dolor sit amet ccc: no pro movet What would be the best way to split the string into an array and get the following result in PHP? array[0]='aaaaa: lorem ipsum'; array[1]='bb: dolor sit amet'; array[2]='ccc: no pro movet'; I can write a function that finds the position of each ":", finds the length of the word before it, and splits the string. But I guess there is an easier way using regular expressions? 回答1: For this kind of job, I

Php preg_split for forwardslash?

五迷三道 提交于 2020-02-06 22:31:08
问题 I've some text that i wish to parse $str = "text1<br/>text2<br/>text3 I've tried using print_r( preg_split("<br/>", $str)); but it is not giving me the desired output 回答1: Try the following: $str = "text1<br/>text2<br/>text3"; print_r(preg_split("/<br\/>/", $str)); I'm assuming missing the closing quote " at the end of the $str = "text1<br/>text2<br/>text3" is just a typo. Take a look at this page on how to specify the string $pattern parameter: http://php.net/manual/en/function.preg-split

preg_split commas not inside parenthesis

泪湿孤枕 提交于 2020-01-21 21:14:11
问题 Test string: Organic whole wheat bread, Monterey Jack Cheese (milk, cheese culture, salt), Hormel Natural Ham (salt, turbinado sugar, lactic acid (not from milk) Desired output: Array ( [0] => Organic whole wheat bread [1] => Monterey Jack Cheese [2] => Hormel Natural Ham ) I don't mind if the sub-ingredients appear with the original item (i.e., "Monterey Jack Cheese (milk, cheese culture, salt)"), I just don't want them on their own because they aren't added ingredients. I found a couple

php preg_split() to find text inbetween two words

帅比萌擦擦* 提交于 2020-01-16 18:34:17
问题 how do I use preg_split() to grab the value between [item:1] and [/item:1] $query= [item:1] my content [/item:1]; This isn't working $match = preg_split('/[[item:1][\/item:1]]/', $query); echo $match[0]; output should be just " my content ". UPDATE: $query = ' [page:1]you got page one content[/page:1] [page:2]you got page two content[/page:2] '; // parse function function parse_page($page, $string) { // VERY IMPORTANT STRIP OUT ANYTHING THAT WOULD SCREW UP THE PARSE $string = str_replace

Combine regular expressions for splitting camelCase string into words

谁说我不能喝 提交于 2020-01-14 10:36:08
问题 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 abreviations like this: 'hasABREVIATIONEmbedded' translates to 'Has ABREVIATION Embedded' I came up with this solution: <?php function camelCaseToWords($camelCaseStr) { // Convert: "TestASAPTestMore" to "TestASAP TestMore" $abreviationsPattern = '/'