preg-split

In PHP, which is faster: preg_split or explode?

一曲冷凌霜 提交于 2019-11-26 23:19:25
问题 This may sound like a stupid question, but: which is faster when using it to extract keywords in a search query in php: $keyword = preg_split('/[\s]+/', $_GET['search']); or $keyword = explode(' ', $_GET['search']); 回答1: Explode is faster, per PHP.net Tip If you don't need the power of regular expressions, you can choose faster (albeit simpler) alternatives like explode() or str_split(). 回答2: In a simple usage explode() is than faster, see: micro-optimization.com/explode-vs-preg_split (link

Split string by delimiter, but not if it is escaped

无人久伴 提交于 2019-11-26 19:27:47
How can I split a string by a delimiter, but not if it is escaped? For example, I have a string: 1|2\|2|3\\|4\\\|4 The delimiter is | and an escaped delimiter is \| . Furthermore I want to ignore escaped backslashes, so in \\| the | would still be a delimiter. So with the above string the result should be: [0] => 1 [1] => 2\|2 [2] => 3\\ [3] => 4\\\|4 Use dark magic: $array = preg_split('~\\\\.(*SKIP)(*FAIL)|\|~s', $string); \\\\. matches a backslash followed by a character, (*SKIP)(*FAIL) skips it and \| matches your delimiter. Instead of split(...) , it's IMO more intuitive to use some sort

Split String into Text and Number

老子叫甜甜 提交于 2019-11-26 17:17:18
问题 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

Split string by delimiter, but not if it is escaped

血红的双手。 提交于 2019-11-26 06:59:36
问题 How can I split a string by a delimiter, but not if it is escaped? For example, I have a string: 1|2\\|2|3\\\\|4\\\\\\|4 The delimiter is | and an escaped delimiter is \\| . Furthermore I want to ignore escaped backslashes, so in \\\\| the | would still be a delimiter. So with the above string the result should be: [0] => 1 [1] => 2\\|2 [2] => 3\\\\ [3] => 4\\\\\\|4 回答1: Use dark magic: $array = preg_split('~\\\\.(*SKIP)(*FAIL)|\|~s', $string); \\\\. matches a backslash followed by a