Explode does not work with multilpe commas inside the string [closed]

 ̄綄美尐妖づ 提交于 2019-12-02 13:42:28

If you want to remove any space and duplicate tags then you need to also add array_unique with array_filter

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

Note .. Please this would not remove the . in the result .. here is a better way to filter your results

$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);

Output

brazil,banks,home,uk,test,financial times,ipad,Two words

What you need is array_unique function:

$Text_Array = array_unique($Text_Array);

So your code becomes:

$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);

and you will then get the desired output.

You could replace multiple continuous commas by a single comma before you explode the string

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

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

print_r($Text_Array);

Note that you will still have one element in your array containing a dot, and the last element of the array will be empty if your string ends with a comma.

You need such kind of code

$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. You need to replace ,, or more with one , I've done it with preg_replace
  2. Remove last , from the string
  3. Explode by ,
  4. Remove duplicates by array_unique()

Try this:

<?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);
?>

You can remove all unncessary commata in your example by replacing ',,' with ',' until no more ',,' exists.

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

Used functions:

In addition to the solution given by these people you can try this to print values as u wanted:-

foreach($Text_Array as $textval)
{
echo $textval . ",";
}

u may try this

<?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 )
?>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!