explode 相关问题

explode()是一个PHP函数,它根据分隔符拆分字符串,返回一个数组。

分解元素数量不匹配的 Pandas 数据框

我正在处理这样的数据集,我想用每个列表的内容来分解它。 索引 col1 col2 col3 1 [A,B] [,xx] [1,2] 2 [A,C] [zz,xx] [3,4] 3...

回答 1 投票 0

PHP 在第一个字母数字字符处分解字符串

我有一个这样的琴弦。 $str = "-=!#?Bob-Green_Smith"; $str = "-_@!?1241482"; 我怎样才能在第一次字母数字匹配时爆炸它们。 例如: $str = "-=!#?Bob-Green_Smith"; 变成: $val[0] =...

回答 3 投票 0

Pyspark 如何避免顶部和嵌套结构中的组爆炸(代码优化)

问题 我想根据请求数据计算一些统计数据,这些数据按顶层中的值和嵌套层中的值分组。 explode-join和3x groupby的主要问题是大数据的代码(100G...

回答 1 投票 0

将管道分隔值数组打印为 HTML 列表项标签

数据来自Wordpress元框,以单个长数组的形式。 我在元框中插入的数据如下所示: 莱茵雷司令1|0,75升|9,50欧元 莱茵雷司令2|0,75升|9,50欧元 铑...

回答 4 投票 0

PHP 在分隔符的第二个实例上爆炸

我正在尝试使用 PHP 分解字符串,但是只有在分解之前检测到分隔符的第二个实例时,对于我的情况,我想在检测到第二个空格后分解它....

回答 5 投票 0

性能比较:在foreach()签名中调用explode()与将爆炸数据作为变量传递给foreach()

foreach(explode(',' $foo) as $bar) { ... } 与 $测试 = 爆炸(',' $foo); foreach($test as $bar) { ... } 在第一个示例中,它是否会为每次迭代分解 $foo 字符串,或者 PHP 是否保留...

回答 4 投票 0

提取后面跟有特定字符的所有数字子字符串

如果我有一个字符串“123x456x78”,我如何分解它以返回一个包含“123”作为第一个元素和“456”作为第二个元素的数组?基本上,我想...

回答 8 投票 0

将字符串分解为数组

我想用不同类型的字符分解一个字符串。 我已经知道如何爆炸一根弦,我也想我知道如何在不同的级别上做到这一点。 问题是你如何使用其中一个 v...

回答 5 投票 0

将字符串中的单词拆分为数组,而不破坏用双引号括起来的短语

我想让用户输入标签:windows linux "mac os x" 然后用空格将它们分开,同时将“mac os x”识别为一个完整的单词。 这可以结合吗...

回答 5 投票 0

将平面数组中的所有分隔字符串分解为单个子字符串的平面数组

我有一个循环,其中包含一串逗号分隔的值。 foreach ($profiles 作为 $profile) { $user_states[] = exlode(', ', '[逗号分隔状态字符串]'); } 我遇到的问题...

回答 7 投票 0

将平面数组中的所有分隔字符串分解为单个子字符串的平面数组

我有一个循环,其中包含一串逗号分隔的值。 foreach ($profiles 作为 $profile) { $user_states[] = exlode(', ', '[逗号分隔状态字符串]'); } 我遇到的问题...

回答 7 投票 0

我应该使用explode还是Unpivot?

我有一个由“无合同”、“过去”、“当前”和“过期”列组成的表格。 +--------+------+--------+--------+ |编号续|过去|当前|已过期| +--------+------+--------+--------+ |113 | X | ...

回答 1 投票 0

将逗号分隔的字符串转换为整数数组?

以下代码: $字符串 = "1,2,3" $ids = 爆炸(',', $string); var_dump($ids); 返回数组: 数组(3){ [0]=> 字符串(1)“1” [1]=> 字符串(1)“2”...

回答 18 投票 0

在 PHP 中分解 url

我对这个功能有疑问。它根据我的选择来排列域,但是有一个问题,前段时间发生了变化。域名是这样的 www.site.com www.sites2.com www.sitett3...

回答 2 投票 0

如果前面有特定子字符串,则按空格分割

$text = "这是/n我的文字/n哇"; $quotes = 爆炸('/n',$text); 这会将字符串拆分为“This is”“My text”“wow” 但我希望它保留字符串“/n”,而不是剪切...

回答 3 投票 0

在字符每隔两次出现时分解字符串

如何在特定字符每隔两次出现时分解一串文本? $text =“在线杂货是少数正在增长的市场渠道之一,尽管其利润丰厚......

回答 4 投票 0

每隔两个点字符分解一个字符串

如何在每隔一个字符上分解一串文本? $text =“在线杂货是少数正在增长的市场渠道之一,尽管其盈利能力值得怀疑。Accord...

回答 4 投票 0

在数组中的特定位置查找字符串 - PHP

我有以下文本字符串:“Gardening,Landscaping,Football,3D Modelling” 我需要 PHP 来挑选出短语“Football”之前的字符串。 因此,无论数组的大小如何,代码都会

回答 4 投票 0

PHP 爆炸函数空白数组元素

我在使用 php 爆炸功能时遇到了一些问题。 我想要爆炸的字符串是: ,.stl,.ppl 目前,我正在使用爆炸功能: 爆炸(',',',.stl,.ppl'); 不幸的是...

回答 2 投票 0

在 PHP 中将字符串分解为数组(字符串类似于 WP 短代码)

我正在使用 Laravel 开发一种页面构建器,并尝试实现 WordPress 风格的短代码。我有 HTML 代码作为字符串,如下所示: $字符串=' [标题... 我正在使用 Laravel 开发一种页面构建器,并尝试实现 WordPress 风格的短代码。我有 HTML 代码作为字符串,如下所示: $string = '<div class="temp"> [title type="text" tag="h2" value="This is my cool title" disabled some-attribute] [text type="text" tag="p" value="Lorem ipsum dolor sit amet"] </div>'; 好吧,现在我想提取所有“短代码”(括号中的内容[...])并将其存储在这样的数组中: [ [ "id" => "title", "type" => "text", "tag" => "h2", "value" => "This is my cool title", "disabled" => true, "some-attribute" => true ], [ "id" => "text", "type" => "text", "tag" => "p", "value" => "Lorem ipsum dolor sit amet" ] ] 我的问题是由于“”中的空格而将短代码字符串分解为数组。 这是我的 PHP 代码: // Extract all brackets from the string preg_match_all('/(?<=\[)[^\][]*(?=])/', $string, $matches); $itemsArray = array(); // Explode each shortcode by spaces foreach ($matches[0] as $match) { $shortcode = explode( ' ', $match ); $itemArray = array(); // split shortcode in attributes foreach ($shortcode as $key => $attr) { $temp = explode('=', $attr); // First attribute is the id if ($key === 0) { $itemArray['id'] = $temp[0]; } else { if ( count($temp) > 1 ) { $itemArray[ $temp[0] ] = trim($temp[1],'"'); } else { $itemArray[ $temp[0] ] = true; } } } $itemsArray[] = $itemArray; } 结果很接近,但引号“”中的空格搞砸了字符串的爆炸: [ [ "id" => "title" "type" => "text" "tag" => "h2" "value" => "This" "is" => true "my" => true "cool" => true "title"" => true "disabled" => true "some-attribute" => true ], [ "id" => "text" "type" => "text" "tag" => "p" "value" => "Lorem" "ipsum" => true "dolor" => true "sit" => true "amet"" => true ] ] 如何排除字符串爆炸中的空格? 也许这对你有用?请告诉我 $string = '<div class="temp"> [title type="text" tag="h2" value="This is my cool title" disabled some-attribute] [text type="text" tag="p" value="Lorem ipsum dolor sit amet"] </div>'; preg_match_all('/\[(\w+)([^]]*)\]/', $string, $matches); $itemsArray = array(); foreach ($matches[0] as $match) { preg_match('/\[(\w+)([^]]*)\]/', $match, $shortcodeMatches); $id = $shortcodeMatches[1]; $attributes = $shortcodeMatches[2]; $itemArray = array(); $itemArray['id'] = $id; preg_match_all('/(\w+)=("[^"]*"|\'[^\']*\'|\S+)/', $attributes, $attributeMatches, PREG_SET_ORDER); foreach ($attributeMatches as $attrMatch) { $key = $attrMatch[1]; $value = trim($attrMatch[2], '"\''); $itemArray[$key] = $value; } $itemsArray[] = $itemArray; } print_r($itemsArray);

回答 1 投票 0

© www.soinside.com 2019 - 2024. All rights reserved.