我是一个使用PHP的脚本语言解释器.我用这种脚本语言编写了这段代码:
write {Hello, World!} in either the color {blue} or {red} or {#00AA00} and in either the font {Arial Black} or {Monaco} where both the color and the font are determined randomly
Run Code Online (Sandbox Code Playgroud)
(是的,很难相信,但那是语法)
我必须使用哪个正则表达式来拆分它(用空格分割),但仅限于不在大括号内.所以我想把上面的代码转换成这个数组:
(大括号内的字符串以粗体显示在上面)大括号内的字符串必须是每个元素.所以{Hello,World!}不能:1.你好,2.世界!
我怎样才能做到这一点?
提前致谢.
我遇到以下内容将字符串拆分为"令牌":
$tokens = preg_split("/[^\-_A-Za-z0-9]+/", $string);
Run Code Online (Sandbox Code Playgroud)
有人可以向我解释这与此有何不同:
$tokens = explode(' ', $string);
Run Code Online (Sandbox Code Playgroud)
任何帮助将不胜感激 :-)
我试图用一个或多个空格分割一个字符串,但下面不起作用..相反,它将整个字符串作为一个单一数组返回。
$str = 'I am a test';
$parts = preg_split('/\s+/', $str, PREG_SPLIT_NO_EMPTY);
print_r($parts);
Run Code Online (Sandbox Code Playgroud)
这是它返回的内容:
Array
(
[0] => I am a test
)
Run Code Online (Sandbox Code Playgroud) 我想使用 preg_split 在双正斜杠 (//) 和问号 (?) 上拆分字符串。我可以像这样使用单个正斜杠:
preg_split('[\/]', $string);
Run Code Online (Sandbox Code Playgroud)
但
preg_split('[\//]', $string);
or
preg_split('[\/\/]', $string);
Run Code Online (Sandbox Code Playgroud)
不工作。
如何在表达式中使用双正斜杠?
我正在尝试使用多个分隔符(?? , ? , ?? , ?)来分解字符串.
例如,我有这个输入字符串:
$string = "(2.8 , 3.1) ? (??2.4 , ?3.0)";
Run Code Online (Sandbox Code Playgroud)
我想将它转换为这样的数组(预期输出):
Array
(
[0] => (2.8 , 3.1) ? (
[1] => ??
[2] => 2.4 ,
[3] => ?
[4] => 3.0)
)
Run Code Online (Sandbox Code Playgroud)
我最好的尝试打印我(当前输出):
Array
(
[0] => (2.8 , 3.1) ? (
[1] => ?
[2] => ?2.4 ,
[3] => ?3.0)
)
Run Code Online (Sandbox Code Playgroud)
这是我目前的代码:
<?php
function multiexplode ($delimiters,$string) {
return explode(
$delimiters[0],
strtr(
$string,
array_combine(
array_slice($delimiters,1),
array_fill(0,count($delimiters)-1,array_shift($delimiters))
)
) …Run Code Online (Sandbox Code Playgroud) 我读了其他相关的帖子,但不可能让它适用于我的情况.
我有以下文字:
This. Is a test. I show. You.
Run Code Online (Sandbox Code Playgroud)
我想使用preg_split使用分隔符'.'(点+空格),但我需要在返回的数组中保留分隔符.这是需要的结果:
array(
'0' => 'This.',
'1' => 'Is a test.',
'2' => 'I show.',
'3' => 'You.',
);
Run Code Online (Sandbox Code Playgroud)
我已经尝试过的:
preg_split("/(\.\s/)", $text, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
Run Code Online (Sandbox Code Playgroud) 我希望我的正则表达式匹配?ver和?v,但不 ?version
这是我到目前为止: $parts = preg_split( "(\b\?ver\b|\b\?v\b)", $src );
我觉得麻烦可能就是我逃避了?.
我可以用逗号分割一个字符串preg_split,比如
$words = preg_split('/[,]/', $string);
Run Code Online (Sandbox Code Playgroud)
如何使用点,空格和分号来分割字符串中的任何一个?
PS.我在PHP preg_split页面上找不到任何相关的例子,这就是我要问的原因.
preg_split在用空格替换字母或数字以外的任何内容之后,我试图用任意数量的空格分割字符串...这是我的代码(包含一些调试内容):
$input = strtolower($data_current[0]);
$input = preg_replace('/[^a-z0-9]/', ' ', $input);
echo($input."\r\n");
$array = preg_split('/[\s]+/', $input, PREG_SPLIT_NO_EMPTY);
print_r($array);
die;
Run Code Online (Sandbox Code Playgroud)
假设 的值为$data_current[0]“hello world”。我得到的输出是这样的......
hello world
array
(
[0] => hello world
)
Run Code Online (Sandbox Code Playgroud)
显然,我期望一个具有两个值的数组......“hello”和“world”。
这里到底发生了什么?如果有帮助的话,$data_current可以从 CSV 中读取数组(使用) ...fgetcsv
我正在寻找一种方法将字符串1 hello there 6 foo 37 bar转换成数组,如:
Array ( [1] => "hello there",
[6] => "foo",
[37] => "bar" )
Run Code Online (Sandbox Code Playgroud)
每个数字都是它后面的字符串的索引.我想得到它的帮助.谢谢!:)