PHP串爆炸,但治疗的话,在报价作为一个单字
-
18-09-2019 - |
题
我怎么能爆炸以下string:
Lorem ipsum "dolor sit amet" consectetur "adipiscing elit" dolor
入
array("Lorem", "ipsum", "dolor sit amet", "consectetur", "adipiscing elit", "dolor")
因此,引号中的文本被视为一个单词。
这是什么我现在:
$mytext = "Lorem ipsum %22dolor sit amet%22 consectetur %22adipiscing elit%22 dolor"
$noquotes = str_replace("%22", "", $mytext");
$newarray = explode(" ", $noquotes);
但我的代码鸿沟的每个字进入阵列。我怎么做的话在引号内处理作为一个单词?
解决方案
您可以使用preg_match_all(...)
:
$text = 'Lorem ipsum "dolor sit amet" consectetur "adipiscing \\"elit" dolor';
preg_match_all('/"(?:\\\\.|[^\\\\"])*"|\S+/', $text, $matches);
print_r($matches);
这将产生:
Array
(
[0] => Array
(
[0] => Lorem
[1] => ipsum
[2] => "dolor sit amet"
[3] => consectetur
[4] => "adipiscing \"elit"
[5] => dolor
)
)
正如你所看到的,这也说明了引号的字符串内转义引号。
修改强>
一个简短的说明:
" # match the character '"'
(?: # start non-capture group 1
\\ # match the character '\'
. # match any character except line breaks
| # OR
[^\\"] # match any character except '\' and '"'
)* # end non-capture group 1 and repeat it zero or more times
" # match the character '"'
| # OR
\S+ # match a non-whitespace character: [^\s] and repeat it one or more times
和匹配%22
,而不是双引号的情况下,你会怎么做:
preg_match_all('/%22(?:\\\\.|(?!%22).)*%22|\S+/', $text, $matches);
其他提示
这本来是与更容易 str_getcsv()
。
$test = 'Lorem ipsum "dolor sit amet" consectetur "adipiscing elit" dolor';
var_dump(str_getcsv($test, ' '));
使你
array(6) {
[0]=>
string(5) "Lorem"
[1]=>
string(5) "ipsum"
[2]=>
string(14) "dolor sit amet"
[3]=>
string(11) "consectetur"
[4]=>
string(15) "adipiscing elit"
[5]=>
string(5) "dolor"
}
您也可以尝试这种多爆炸功能
function multiexplode ($delimiters,$string)
{
$ready = str_replace($delimiters, $delimiters[0], $string);
$launch = explode($delimiters[0], $ready);
return $launch;
}
$text = "here is a sample: this text, and this will be exploded. this also | this one too :)";
$exploded = multiexplode(array(",",".","|",":"),$text);
print_r($exploded);
在某些情况下,鲜为人知 token_get_all()
可能证明是有用的:
$tokens = token_get_all("<?php $text ?>");
$separator = ' ';
$items = array();
$item = "";
$last = count($tokens) - 1;
foreach($tokens as $index => $token) {
if($index != 0 && $index != $last) {
if(count($token) == 3) {
if($token[0] == T_CONSTANT_ENCAPSED_STRING) {
$token = substr($token[1], 1, -1);
} else {
$token = $token[1];
}
}
if($token == $separator) {
$items[] = $item;
$item = "";
} else {
$item .= $token;
}
}
}
结果:
Array
(
[0] => Lorem
[1] => ipsum
[2] => dolor sit amet
[3] => consectetur
[4] => adipiscing elit
[5] => dolor
)
我来到这里,与一个复杂的串分裂的问题与此类似,但没有答复在这里也正是我想要的-所以我写了我自己。
我张贴在这里,只是在情况是有帮助的其他人。
这可能是一个非常缓慢和效率低下的方法来做到这一点-但是这对我的作品。
function explode_adv($openers, $closers, $togglers, $delimiters, $str)
{
$chars = str_split($str);
$parts = [];
$nextpart = "";
$toggle_states = array_fill_keys($togglers, false); // true = now inside, false = now outside
$depth = 0;
foreach($chars as $char)
{
if(in_array($char, $openers))
$depth++;
elseif(in_array($char, $closers))
$depth--;
elseif(in_array($char, $togglers))
{
if($toggle_states[$char])
$depth--; // we are inside a toggle block, leave it and decrease the depth
else
// we are outside a toggle block, enter it and increase the depth
$depth++;
// invert the toggle block state
$toggle_states[$char] = !$toggle_states[$char];
}
else
$nextpart .= $char;
if($depth < 0) $depth = 0;
if(in_array($char, $delimiters) &&
$depth == 0 &&
!in_array($char, $closers))
{
$parts[] = substr($nextpart, 0, -1);
$nextpart = "";
}
}
if(strlen($nextpart) > 0)
$parts[] = $nextpart;
return $parts;
}
使用情况如下。 explode_adv
5参数:
- 一系列人物打开一个方框-例如
[
,(
, 等等。 - 一系列人物,附近的一块-例如
]
,)
, 等等。 - 一系列人物,切换一个方框-例如
"
,'
, 等等。 - 一系列符的,应该导致一个分成未来的一部分。
- 串的工作。
这种方法可能有缺陷的编辑都是受欢迎的。
不隶属于 StackOverflow