-4

我试图在这里和谷歌中找到答案,但没有运气!

为什么当我尝试爆炸字符串时它不起作用?

$Text = "brazil,banks,home,,uk,,,,test,financial times,.,ipad,,banks,,Two words,,";

$Text_Array = explode(",",$Text);

$Text_Array = array_filter($Text_Array);

print_r($Text_Array);

我想让我的所有标签除以一个逗号并删除任何空格和重复标签。

我想要这个结果:

“巴西,银行,首页,英国,测试,金融时报,ipad,两个字”;

拜托,你能帮我实现这个目标吗?

4

8 回答 8

3

如果你愿意,那么remove any space and duplicate tags你还需要添加array_uniquearray_filter

$textAray = array_unique(array_filter($textAray));

注意..请这不会删除.结果中的..这是过滤结果的更好方法

$text = "brazil,banks,home,,uk,,,,test,financial times,.,ipad,,banks,,Two words,,";
$textArray = array_unique(preg_split("/[,.]+/", $text));
$textArray = array_filter($textArray);
echo implode(",", $textArray);

输出

brazil,banks,home,uk,test,financial times,ipad,Two words
于 2013-05-22T08:54:22.500 回答
0

你需要的是 array_unique 函数:

$Text_Array = array_unique($Text_Array);

所以你的代码变成:

$Text = "brazil,banks,home,,uk,,,,test,financial times,.,ipad,,banks,,Two words,,";

$Text_Array = explode(",",$Text);

$Text_Array = array_filter($Text_Array);

$Text_Array = array_unique($Text_Array);\

print_r($Text_Array);

然后您将获得所需的输出。

于 2013-05-22T08:54:44.487 回答
0

您可以在分解字符串之前用单个逗号替换多个连续逗号

$Text = "brazil,banks,home,,uk,,,,test,financial times,.,ipad,,banks,,Two words,,";

$Text = preg_replace("/,+/", ",", $Text);
$Text_Array = explode(",",$Text);

print_r($Text_Array);

请注意,您的数组中仍有一个包含点的元素,如果您的字符串以逗号结尾,则数组的最后一个元素将为空。

于 2013-05-22T08:54:57.743 回答
0

你需要这样的代码

$Text = "brazil,banks,home,,uk,,,,test,financial times,.,ipad,,banks,,Two words,,";

$str = substr(preg_replace('/(.+?)[,]+/', '$1,', $Text),0,strlen($str)-1);
$arr = array_unique(explode(',', $str));
print_r($arr);
  1. 您需要用一个替换 , 或多个,我已经用 preg_replace 完成了
  2. 从字符串中删除最后一个
  3. 爆炸,
  4. 通过 array_unique() 删除重复项
于 2013-05-22T08:56:16.393 回答
0

试试这个:

<?php
$Text = "brazil,banks,home,,uk,,,,test,financial times,.,ipad,,banks,,Two words,,";
$Text = str_replace('.','',implode(',',array_unique(explode(',', $Text))));
$Text_Array = explode(",",$Text);

$Text_Array = array_filter($Text_Array);

print_r($Text_Array);
?>
于 2013-05-22T08:58:06.700 回答
0

除了这些人给出的解决方案之外,您还可以尝试使用它来打印您想要的值:-

foreach($Text_Array as $textval)
{
echo $textval . ",";
}
于 2013-05-22T09:04:27.217 回答
-1

你可以试试这个

<?php

$Text = "brazil,banks,home,,uk,,,,test,financial times,.,ipad,,banks,,Two words,,";

$Text=str_replace(array(",,",",,,,",",.,",",,"),',',$Text);

$Text_Array = explode(",",$Text);

$Text_Array = array_filter($Text_Array);

print_r($Text_Array);
//output :: Array ( [0] => brazil [1] => banks [2] => home [3] => uk [4] => test [5] => financial times [6] => ipad [7] => banks [8] => Two words )
?>
于 2013-05-22T08:56:20.150 回答
-1

您可以通过将 ',,' 替换为 ',' 直到不再存在 ',,' 来删除示例中所有不必要的逗号。

while ( strstr($Text,',,') !== false ) {
    $Text = str_replace($Text,',,',',');
}

使用的功能:

于 2013-05-22T08:58:37.967 回答