问题
I try to find the answer here and in Google but no luck!
Why it does not work when I try to explode the string?
$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);
I would like to have all my tags divide by ONE comma and remove any space and duplicate tags.
I would like this result:
"brazil,banks,home,uk,test,financial times,ipad,Two words";
Please, can you help me to achieve this?
回答1:
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
回答2:
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.
回答3:
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.
回答4:
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);
- You need to replace ,, or more with one , I've done it with preg_replace
- Remove last , from the string
- Explode by ,
- Remove duplicates by array_unique()
回答5:
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);
?>
回答6:
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:
- strstr
- str_replace
回答7:
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 . ",";
}
回答8:
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 )
?>
来源:https://stackoverflow.com/questions/16687389/explode-does-not-work-with-multilpe-commas-inside-the-string