为什么我不能立即访问返回的数组中的元素explode()?
例如,这不起作用:
$username = explode('.',$thread_user)[1];
//Parse error: syntax error, unexpected '[
Run Code Online (Sandbox Code Playgroud)
但是这段代码确实:
$username = explode('.',$thread_user);
$username = $username[1];
Run Code Online (Sandbox Code Playgroud)
我通常不用PHP编程,所以这对我来说相当混乱.
当我必须解析文本(例如配置文件或其他相当简单/描述性的语言)时,我会想到几种解决方案:
strtok(),sscanf()explode()我曾经写过的纯粹无聊的功能lex/ yacc(读取:flex/ bison)生成适当的解析器我不喜欢"库函数"的方法.感觉笨拙和尴尬.explode()虽然它不需要太多新代码,但感觉更加惊慌失措.并且flex/ bison经常看起来纯粹矫枉过正.
我通常会实现一个FSM,但与此同时,我已经为可能需要在稍后维护我的代码的可怜人员感到遗憾.
因此我的问题是:
解析相对简单的文本文件的最佳方法是什么?
它有关系吗?
是否有共同商定的方法?
我有以下结构的字符串:
7_string_12
7_string2_122
7_string3_1223
Run Code Online (Sandbox Code Playgroud)
如何在第二个"_"之前获得字符串?
我希望我的最终结果是:
7_string
7_string2
7_string3
Run Code Online (Sandbox Code Playgroud)
我正在使用explode('_',$ string)并结合前两个值,但我的脚本非常慢!
我认为这段代码在最后放了一个空行。如果这是真的,如何避免这种情况?
$text = explode( "\n", $text );
foreach( $text as $str ) { echo $str; }
Run Code Online (Sandbox Code Playgroud) 我想将标签上的字符串拆分成不同的部分.
$string = 'Text <img src="hello.png" /> other text.';
Run Code Online (Sandbox Code Playgroud)
下一个功能还没有以正确的方式工作.
$array = preg_split('/<img .*>/i', $string);
Run Code Online (Sandbox Code Playgroud)
输出应该是
array(
0 => 'Text ',
1 => '<img src="hello.png" />',
3 => ' other text.'
)
Run Code Online (Sandbox Code Playgroud)
我应该用什么样的模式来完成它?
编辑 如果有多个标签怎么办?
$string = 'Text <img src="hello.png" > hello <img src="bye.png" /> other text.';
$array = preg_split('/(<img .*>)/i', $string, -1, PREG_SPLIT_DELIM_CAPTURE);
Run Code Online (Sandbox Code Playgroud)
输出应该是:
array (
0 => 'Text ',
1 => '<img src="hello.png" />',
3 => 'hello ',
4 => '<img src="bye.png" />',
5 => ' other text.' …Run Code Online (Sandbox Code Playgroud) 我正在尝试explode用逗号,分隔字符串:但仅当它不在括号之间时(...)
这是一些代码来显示我的意思:
$string = "K.VisueelNr,IFNULL(P.Partijnaam, 'Leeg gemeld') AS Partijnaam,R.Ras,M.Maat,DATE_FORMAT(KS.Timestamp, '%d-%c-%Y') AS Datum,H.Handelshuis,KS.Actie";
print_r(str_getcsv($string, '()'));
print_r(explode(",", $string));
Run Code Online (Sandbox Code Playgroud)
这将输出:
Array
(
[0] => K.VisueelNr,IFNULL
[1] => P.Partijnaam, 'Leeg gemeld') AS Partijnaam,R.Ras,M.Maat,DATE_FORMAT
[2] => KS.Timestamp, '%d-%c-%Y') AS Datum,H.Handelshuis,KS.Actie
)
Array
(
[0] => K.VisueelNr
[1] => IFNULL(P.Partijnaam
[2] => 'Leeg gemeld') AS Partijnaam
[3] => R.Ras
[4] => M.Maat
[5] => DATE_FORMAT(KS.Timestamp
[6] => '%d-%c-%Y') AS Datum
[7] => H.Handelshuis
[8] => KS.Actie
)
Run Code Online (Sandbox Code Playgroud)
但我正在寻找这样的输出:
Array
(
[0] …Run Code Online (Sandbox Code Playgroud) 我在Scala中使用spark库.我已经创建了一个DataFrame
val searchArr = Array(
StructField("log",IntegerType,true),
StructField("user", StructType(Array(
StructField("date",StringType,true),
StructField("ua",StringType,true),
StructField("ui",LongType,true))),true),
StructField("what",StructType(Array(
StructField("q1",ArrayType(IntegerType, true),true),
StructField("q2",ArrayType(IntegerType, true),true),
StructField("sid",StringType,true),
StructField("url",StringType,true))),true),
StructField("where",StructType(Array(
StructField("o1",IntegerType,true),
StructField("o2",IntegerType,true))),true)
)
val searchSt = new StructType(searchArr)
val searchData = sqlContext.jsonFile(searchPath, searchSt)
Run Code Online (Sandbox Code Playgroud)
我现在要爆炸什么东西what.q1,它应该包含一个整数数组,但文档是有限的:http://spark.apache.org/docs/1.4.0/api/java/org/apache/ 火花/ SQL/DataFrame.html#爆炸(java.lang.String中,%20java.lang.String,%20scala.Function1,%20scala.reflect.api.TypeTags.TypeTag)
到目前为止,我尝试了一些没有太多运气的东西
val searchSplit = searchData.explode("q1", "rb")(q1 => q1.getList[Int](0).toArray())
Run Code Online (Sandbox Code Playgroud)
有关如何使用爆炸的任何想法/示例?
对于我当前的项目,我需要用户定义存储在数据库中的一系列数字.
以下字符串是可能的用户输入:
1025-1027,1030,1032-1034
Run Code Online (Sandbox Code Playgroud)
我想用php处理这个字符串以获得一系列可能的数字,包括添加一系列数字n-n²或添加单个数字的可能性n; n²,这个数字分别用于此示例:
1025 1026 1027 1030 1032 1031 1034
Run Code Online (Sandbox Code Playgroud) 重要编辑:由于许多人说应该避免这种情况并且几乎无法使用RegEx,我将允许您提供其他一些解决方案.从现在开始,任何解决方案都可以用作答案,最后是解决方案.谢谢!
让我们说:
$line = "{ It is { raining { and streets are wet } | snowing { and streets are { slippy | white }}}. Tomorrow will be nice { weather | walk }. }"
Run Code Online (Sandbox Code Playgroud)
期望的输出:
It is raining and streets are wet. Tomorrow will be nice weather.
It is raining and streets are wet. Tomorrow will be nice walk.
It is snowing and streets are slippy. Tomorrow will be nice weather.
It is snowing and streets are …Run Code Online (Sandbox Code Playgroud) 从 CSV 文件(带有标题和管道分隔符)我得到了以下两个内容,其中包含一个 JSON 列(里面有一个集合),如下所示:
第一种情况(使用没有名称的 JSON 集合):
ProductId|IngestTime|ProductOrders
9180|20171025145034|[{"OrderId":"299","Location":"NY"},{"OrderId":"499","Location":"LA"}]
8251|20171026114034|[{"OrderId":"1799","Location":"London"}]
Run Code Online (Sandbox Code Playgroud)
第二种情况(使用名为“Orders”的 JSON 集合):
ProductId|IngestTime|ProductOrders
9180|20171025145034|{"Orders":[{"OrderId":"299","Location":"NY"},{"OrderId":"499","Location":"LA"}]}
8251|20171026114034|{"Orders":[{"OrderId":"1799","Location":"London"}]}
Run Code Online (Sandbox Code Playgroud)
首先,我像这样创建我的“原始”表:
DROP TABLE IF EXISTS Product;
CREATE EXTERNAL TABLE Product (
ProductId STRING,
IngestTime STRING,
ProductOrders STRING
)
COMMENT "Product raw table"
ROW FORMAT DELIMITED
FIELDS TERMINATED BY '\|'
STORED AS TEXTFILE
LOCATION
'/data/product'
TBLPROPERTIES ("skip.header.line.count"="1");
Run Code Online (Sandbox Code Playgroud)
当我查询我的表时:
SELECT * FROM Product
Run Code Online (Sandbox Code Playgroud)
我有以下答案:
第一种情况(使用没有名称的 JSON 集合):
ProductId IngestTime ProductOrders
9180 20171025145034 [{"OrderId":"299","Location":"NY"},{"OrderId":"499","Location":"LA"}]
8251 20171026114034 [{"OrderId":"1799","Location":"London"}]
Run Code Online (Sandbox Code Playgroud)
第二种情况(使用名为“Orders”的 JSON 集合):
ProductId IngestTime ProductOrders
9180 20171025145034 {"Orders":[{"OrderId":"299","Location":"NY"},{"OrderId":"499","Location":"LA"}]} …Run Code Online (Sandbox Code Playgroud)