我有以下结构的字符串:
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)并结合前两个值,但我的脚本非常慢!
我们可以在PHP中进行多次explode()吗?
例如,要这样做:
foreach(explode(" ",$sms['sms_text']) as $no)
foreach(explode("&",$sms['sms_text']) as $no)
foreach(explode(",",$sms['sms_text']) as $no)
Run Code Online (Sandbox Code Playgroud)
一体化爆炸像这样:
foreach(explode('','&',',',$sms['sms_text']) as $no)
Run Code Online (Sandbox Code Playgroud)
最好的方法是什么?我想要的是在一行中的多个分隔符上拆分字符串.
我想从我的数据库中检索一些标签,它们的形式如下:
topic_id tags
1 `tag1,tag2,tag3`
2 `tag1,tag4,tag5`
3 `tag2,tag4,tag5`
4 `tag6,tag7,tag2`
Run Code Online (Sandbox Code Playgroud)
我想要这样的东西:
tag1 tag2 tag3 tag4 tag5 tag6 tag7
Run Code Online (Sandbox Code Playgroud)
即所有独特的标签
这样我就可以在链接中包装每个标记,以便对具有此类特定标记的新闻文章进行分组.
我到目前为止写的以下查询无法正常工作:
$tags = mysql_query("SELECT tags, topic_id
FROM forum_topics
WHERE topic_id > 0") or die (mysql_error());
while($tag = mysql_fetch_assoc($tags)){
$split_tags = "$tag";
$pieces = explode(",", $split_tags);
echo $pieces ;
Run Code Online (Sandbox Code Playgroud)
我什么时候做的 print_r($pieces);
我有 Array ( [0] => Array ) Array ( [0] => Array ) Array ( [0] => Array ) Array ( [0] => Array )
这不是我想要的.
因为现在我的表结构看起来像这样topic_id , …
我在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)
有关如何使用爆炸的任何想法/示例?
我正在开发一个简单的博客/书签平台,我想添加一个标签,探险家/向下钻取功能点菜美味,以允许用户过滤帖子指定特定标签的列表.
像这样的东西:

使用此简化模型在数据存储区中表示帖子:
class Post(db.Model):
title = db.StringProperty(required = True)
link = db.LinkProperty(required = True)
description = db.StringProperty(required = True)
tags = db.ListProperty(str)
created = db.DateTimeProperty(required = True, auto_now_add = True)
Run Code Online (Sandbox Code Playgroud)
Post的标签存储在ListProperty中,为了检索标记有特定标签列表的帖子列表,Post模型公开了以下静态方法:
@staticmethod
def get_posts(limit, offset, tags_filter = []):
posts = Post.all()
for tag in tags_filter:
if tag:
posts.filter('tags', tag)
return posts.fetch(limit = limit, offset = offset)
Run Code Online (Sandbox Code Playgroud)
虽然我没有太多强调,但效果很好.
当我尝试向方法添加"排序"顺序get_posts以保持按"-created"日期排序结果时,问题就出现了:
@staticmethod
def get_posts(limit, offset, tags_filter = []):
posts = Post.all()
for …Run Code Online (Sandbox Code Playgroud) python indexing google-app-engine explode google-cloud-datastore
是否有理由使用需要数组的字符串?
我有一个开发人员想要传递一个被删除的字符串,在数据库类方法中将其爆炸以从2个单独的字符串中提取多个字段/值对.我想不出没有发送字符串数组的理由.
例:
$s = "value1|value2|value3" ;
$s2 = explode("|", $s);
Run Code Online (Sandbox Code Playgroud)
vs一个数组
$s3 = array(
"1" => "value1",
"2" => "value2",
);
Run Code Online (Sandbox Code Playgroud)
我知道这个想法是实际传递未知数量的键/值对,但我不认为你爆炸的字符串就是答案.但我可能是错的.
我有一个字符串,如下所示:
$string = "New video gameSome TV showAnother item";
Run Code Online (Sandbox Code Playgroud)
我希望能够单独访问每个项目,以获得类似以下输出:
New video game
Some TV show
Another item
Run Code Online (Sandbox Code Playgroud)
如何在字符串或其他随机字符中的每个项目名称之后添加\n,以后我可以将其分解为数组以单独访问字符串中的每个项目?
所以我有一个字符串,我正在变成一个数组,但我想用正则表达式分隔每个单词.我使用下面的函数匹配整个单词.
function substr_count_array($haystack, $needle)
{
$initial = 0;
$bits = explode(' ', $haystack);
foreach ($needle as $substring)
{
if (!in_array($substring, $bits))
{
continue;
}
$initial += substr_count($haystack, $substring);
}
return $initial;
}
Run Code Online (Sandbox Code Playgroud)
问题是它匹配字符串animal,但不是animals.如果我像这样做部分匹配:
function substr_count_array2($haystack, $needle)
{
$initial = 0;
foreach ($needle as $substring)
{
$initial += substr_count($haystack, $substring);
}
return $initial;
}
Run Code Online (Sandbox Code Playgroud)
它也匹配,a因为它包含了单词animals和返回2.如何explode()使用正则表达式作为分隔符,以便我可以匹配每个长度为5-7个字符的字符串?
解释得更简单:
$animals = array('cat','dog','bird');
$toString = implode(' ', $animals);
$data = array('a');
echo substr_count_array($toString, …Run Code Online (Sandbox Code Playgroud) 我怎样才能返回TLD域名
例如,如果我有以下内容:
www.domain.co.uk 我想要回归 .co.uk
同样的 domain.co.uk
和其他所有人一样 TLDs (.com, .co, .org etc)
是否有一种简单的方法可以在PHP中执行此操作而不使用 Regex
我正在考虑使用,explode但我不太熟悉
我正在尝试使用嵌套字段来展平现有数据框的模式。我的数据框的结构是这样的:
root
|-- Id: long (nullable = true)
|-- Type: string (nullable = true)
|-- Uri: string (nullable = true)
|-- Type: array (nullable = true)
| |-- element: string (containsNull = true)
|-- Gender: array (nullable = true)
| |-- element: string (containsNull = true)
Run Code Online (Sandbox Code Playgroud)
类型和性别可以包含元素数组、一个元素或空值。我尝试使用以下代码:
var resDf = df.withColumn("FlatType", explode(df("Type")))
Run Code Online (Sandbox Code Playgroud)
但结果是在生成的数据框中,我丢失了类型列具有空值的行。这意味着,例如,如果我有 10 行,并且在 7 行中类型为 null,在 3 行中类型不为 null,则在我在结果数据框中使用爆炸后,我只有三行。
如何保留具有空值的行但分解值数组?
我找到了某种解决方法,但仍然停留在一个地方。对于标准类型,我们可以执行以下操作:
def customExplode(df: DataFrame, field: String, colType: String): org.apache.spark.sql.Column = {
var exploded = None: Option[org.apache.spark.sql.Column]
colType.toLowerCase() match …Run Code Online (Sandbox Code Playgroud)