preg-split

Extract whatever is in brackets using regular expressions

不问归期 提交于 2019-12-08 05:40:25
问题 I really don't understand regex at all, and it hurts my head. I've a bit of text which looks like this blah blah blah (here is the bit I'd like to extract) ...and I don't really understand how to extract this using PHP's preg_split, or equivalent, command. How do I do this? And where's a good place to understand how preg works? 回答1: Something like this should do the trick, to match what is between ( and ) : $str = "blah blah blah (here is the bit I'd like to extract)"; if (preg_match('/\(([^\

Splitting up html code tags and content

眉间皱痕 提交于 2019-12-08 03:58:16
问题 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. <p>Some content <a href="www.test.com">A link</a></p> Is seperated like this: array = { [0]=>"<p>", [1]=>"Some", [2]=>"content", [3]=>"<a href='www.test.com'>, [4]=>"A", [5]=>"Link", [6]=>"</a>", [7]=>"</p>" I've been using preg_split so far and have either successfully managed to split the string by whitespace or split by tags - but then all the

php preg_split error when switching from split to preg_split

耗尽温柔 提交于 2019-12-07 08:00:34
问题 I get this warning from php after the change from split to preg_split for php 5.3 compatibility : PHP Warning: preg_split(): Delimiter must not be alphanumeric or backslash the php code is : $statements = preg_split("\\s*;\\s*", $content); How can I fix the regex to not use anymore \ Thanks! 回答1: The error is because you need a delimiter character around your regular expression. $statements = preg_split("/\s*;\s*/", $content); 回答2: Although the question was tagged as answered two minutes

PHP preg_split remove commas and trailing white-space

不羁岁月 提交于 2019-12-06 14:18:16
问题 The code below has been taken directly from PHP: preg_split - Manual Example #1 preg_split() example : Get the parts of a search string <?php // split the phrase by any number of commas or space characters, // which include " ", \r, \t, \n and \f $keywords = preg_split("/[\s,]+/", "hypertext language, programming"); print_r($keywords); ?> The above example will output: Array ( [0] => hypertext [1] => language [2] => programming ) I am creating a tagging system which will allow people to type

How can I adapt my regex to allow for escaped quotes?

大兔子大兔子 提交于 2019-12-06 10:08:33
问题 Introduction First my general issue is that I want to string replace question marks in a string, but only when they are not quoted. So I found a similar answer on SO (link) and began testing out the code. Unfortunately, of course, the code does not take into account escaped quotes. For example: $string = 'hello="is it me your are looking for\\"?" AND test=?'; I have adapted a regular expression and code from that answer to the question: How to replace words outside double and single quotes,

PHP add quotes to array for json_decode

﹥>﹥吖頭↗ 提交于 2019-12-06 04:41:01
问题 I'm looking to json_decode a string, but running into a problem with the array elements not having quotes. JSON {"Status":"DISPUTED","GUID":[]} {"Status":"CONFIRMED","GUID":[G018712, G017623]} PHP $json = '{"Status":"CONFIRMED","GUID":[G018712,G017623]}'; $a = json_decode($json, true); print $a['Status']; Results The php print above won't display anything because there are letters mixed in with the numerics within the array and the json_decode doesn't like it. How would you add strings to

PHP preg_split with two delimiters unless a delimiter is within quotes

不打扰是莪最后的温柔 提交于 2019-12-06 00:28:26
Further on from my previous question about preg_split which was answers super fast, thanks to nick; I would really like to extend the scenario to no split the string when a delimiter is within quotes. For example: If I have the string foo = bar AND bar=foo OR foobar="foo bar" , I'd wish to split the sting on every space or = character but include the = character in the returned array (which works great currently), but I don't want to split the string either of the delimiters are within quotes. I've got this so far: <!doctype html> <?php $string = 'foo = bar AND bar=foo'; $array = preg_split('/

PHP preg_split remove commas and trailing white-space

家住魔仙堡 提交于 2019-12-04 18:22:39
The code below has been taken directly from PHP: preg_split - Manual Example #1 preg_split() example : Get the parts of a search string <?php // split the phrase by any number of commas or space characters, // which include " ", \r, \t, \n and \f $keywords = preg_split("/[\s,]+/", "hypertext language, programming"); print_r($keywords); ?> The above example will output: Array ( [0] => hypertext [1] => language [2] => programming ) I am creating a tagging system which will allow people to type anything into a text box and upon return the text will be processed and inserted into the database. The

How can I adapt my regex to allow for escaped quotes?

百般思念 提交于 2019-12-04 17:58:14
Introduction First my general issue is that I want to string replace question marks in a string, but only when they are not quoted. So I found a similar answer on SO ( link ) and began testing out the code. Unfortunately, of course, the code does not take into account escaped quotes. For example: $string = 'hello="is it me your are looking for\\"?" AND test=?'; I have adapted a regular expression and code from that answer to the question: How to replace words outside double and single quotes , which is reproduced here for ease of reading my question: <?php function str_replace_outside_quotes(

Splitting string array based upon digits in php? [closed]

╄→гoц情女王★ 提交于 2019-12-04 07:05:22
问题 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 6 years ago . I have a string $str = "101WE3P-1An Electrically-Small104TU5A-3,Signal-Interference Duplexers Gomez-GarciaRobertoTU5A-3-01" I want to split this string by the numbers, eg:"101WE3P-1An.... "should be first element