How will I compare with one single string? Ex. "thomas" and "thomas" (2 times) is preferably only "thomas" (1 time)

Example:

$name = "john";
//conversion (array, string, for, preg_match or something)
echo $name; //equal "john";

Or example:

$name = "jon john john thomas thomas tom-erick tommy
        john tom-erick mads";
//conversion (array, string, for, preg_match or something)
echo $name;
/*equal
"john
jon
mads
thomas
tom-erick
tommy
"

Well, something loosely like this should work:

$name = "jon john john thomas thomas tom-erick tommy john tom-erick mads";
$names = array_unique(explode(" ", $name));  //  Explodes the names into a unique array
foreach($names as $one_name) {
  echo $one_name . "<br>";
}

Not tested, just off the top of my head…

Yes, almost there. The left is fine now, but the right is not fine, see the picture. error1|690x356

Why?

I have no idea what you mean…
The code I posted shows you how to display only UNIQUE tokens or names.
I do not see any code for the right side of the picture. We deal with code only here, not pictures of results of code. Show us the code you are using and we can help you sort it out.

I try this:

$name="jon john john thomas thomas tom-erick tommy john tom-erick mads"

that is fine, all is correct:

"jon
john
thomas
tom-erick
tommy
mads".

But the other hand, there is an error:

$name="jon john john
                  thomas     thomas tom-erick tommy
            john tom-erick mads"

So that is error,

"jon (correct)
john (correct)
john (error)
thomas (correct)    
thomas (error)
tom-erick (correct)
tommy (correct)
john (error)
tom-erick (error)
mads (correct)"

Here you can see:

<form action="" method="post">
    <textarea name="name">
    </textarea>
    <br><input type="submit" value="Subscribe">
</form>

<?php
$name=$_POST["name"];

$names = array_unique(explode(" ", $name));  //  Explodes the names into a unique array
foreach($names as $one_name) {
    echo $one_name . "<br>";
}

Well, you changed the options… I can not help you if you don’t explain all the issues…

The new one is extra spaces… So, you would have to replace the extra spaces with only one space.
To do that, you need to use the str_replace() funtion in a loop to remove extra spaces. So, you would need to remove the extra spaces by converting all double-spaces into one-space. Then, that issue would go away.

Does this make sense to you?

Ok, forget it on two or more spaces, at least now. If you have only one space (whitespace and/or newline), not two or more spaces.

input:

$name="jon john john thomas thomas tom-erick
tommy john
tom-erick mads"

...
$name = $_POST["name"];
$name = str_replace(" ", "", $name); //new, but failed
/*
    or also new, but failed:
    $name=str_replace("", " ", $name);
    $name=str_replace("", "<br>", $name);
    $name=str_replace("<br>", "", $name);
    also I have tried nl2br(), \r\n, PHP_EOL, etc
*/

$names = array_unique(explode(" ", $name)); 
...

but I don’t know.

output:

"jon
john
thomas
tom-erick
tommy
john tom-erick (error)
mads"

You probably want to tokenize the string via a RegEx.

Try preg_split and then array_unique

OP, please tell us what the real problem is you are trying to solve with this code? What is the high level overview of what you have going on? What is the source of your data?

...   
$name = preg_split("\s", $name);
print_r($name); //blank space

Also try $name = preg_split("/[\s,]+/", $name);. So can you help me?

without preg_split, Input:

$name="jon john john thomas thomas tom-erick
tommy john tom-erick mads"; //2 newlines

Output:

$name="jon
john
thomas
tom-erick tommy //error, tom-erick and tommy on single line
tom-erick //error, two times for "tom-erick"
mads"

What is the actual use case for this? As @benanamen asked. Or is this a homework assignment that you are just struggling to understand how to do?

Doing hobby on myself.

  • Input: one ordinary space. Output: newlines, one by one (thanks to ErnieAlex)
  • Unique on one ordinary space, so Tom and Tom (2 times input) is only Tom (1 time output) (thanks to ErnieAlex)
  • Alphabet, so ex. Input: jon john john…, Output: john
    jon
    mads… //sort($names);

Finished.

  • Input: Many ordinary spaces
  • Input: Newlines

Not finished yet.

Can you help me?

Do you mean that you wish to sort the array? There is a command for that. Just use something like this:

$names = asort(array_unique(explode(" ", $name)) );

This will take the uinque array of names and sort it for you. Simple to do…

This is finished with sort (alphabet), but error with whitespace, i.e.

input
“jon john john thomas thomas
tom-erick tommy john tom-erick mads”

output
“john
jon
mads
thomas
thomas tom-erick (error)
tom-erick
tommy”

<?php
$name=$_POST["name"];
$names = array_unique(explode(" ", $name));  //Explodes the names into a unique array
sort($names);
foreach($names as $one_name) {
    echo $one_name . "<br>";
}

but this is blank with asort:

<?php
$name = $_POST["name"];
$names = asort(array_unique(explode(" ", $name)) );
foreach($names as $one_name) {
    echo $one_name . "<br>";
}

:confused:

Well, I have no idea what you are really attempting to process here.
It is not logical. First, you can not parse data based on spaces and have some of the data stay in place.
Normally, if you have names inputted, they are entered one at a time from some sort of form or database.
And, then you would already have them separated. Parsing data from a string can not be handled in the manner that we coded so far. Unless you are saying that the error was due to having a dash in the text.
In that case, you could check for that, but, what exactly do you need this to do? Confused…

So, did you just invent some random problem and are trying to learn something? This whole thing just doesn’t make sense. If this were for an actual problem, the real problem would be somewhere else.

I try a different way: Almost all the characters are the same, but there is only white space and newlines. No matter if its first names, surnames, web pages, etc. But let say input and newlines (<textarea>):

google.com yahoo.com
bing.com google.com
translate.google.com bing.com/translator phphelp.com
news.yahoo.com twitter.com twitter.com/home
instagram.com/direct/inbox
twitter.com/home
instagram.com/direct/inbox/
instagram.com/direct/inbox/

Here is output (unique, sort, newlines and white space):

bing.com
bing.com/translator
google.com
instagram.com/direct/inbox
instagram.com/direct/inbox/
news.yahoo.com
phphelp.com
translate.google.com
twitter.com
twitter.com/home
yahoo.com

In the real time, I have a bunch of random web pages on input, not only few web pages. Does it make sense, or no?
Btw, I am not English

<?php

$data = 'google.com yahoo.com
bing.com google.com
translate.google.com bing.com/translator phphelp.com
news.yahoo.com twitter.com twitter.com/home
instagram.com/direct/inbox
twitter.com/home
instagram.com/direct/inbox/
instagram.com/direct/inbox/';

$text = str_replace("\n", " ", $data);
$text = explode(" ", $text);

asort($text);
echo '<pre>', print_r($text, true), '</pre>';

Result

Array
(
    [2] => bing.com
    [5] => bing.com/translator
    [0] => google.com
    [3] => google.com
    [10] => instagram.com/direct/inbox
    [13] => instagram.com/direct/inbox/
    [12] => instagram.com/direct/inbox/
    [7] => news.yahoo.com
    [6] => phphelp.com
    [4] => translate.google.com
    [8] => twitter.com
    [9] => twitter.com/home
    [11] => twitter.com/home
    [1] => yahoo.com
)

Well, NOW we understand… So, Benanaman’s solution should work for you.

Thank you

<form action="" method="post">
<textarea name="data">
</textarea>
<br><input type="submit" value="Subscribe">
</form>

<?php
$data = $_POST["data"];
$text = str_replace("\r\n", " ", $data);
$text = array_unique(explode(" ", $text));
asort($text); //this is case sensitive, natcasesort is case insensitive
//echo '<pre>', print_r($text, true), '</pre>';

foreach($text as $one_text) {
    if($one_text)
        echo $one_text."<br>";
}
Sponsor our Newsletter | Privacy Policy | Terms of Service