Jump to content

Get a few rows from a textarea and remove lines that don't contain a comma


Notreallybrad

Recommended Posts

What I want to achieve:

 

1. User pastes around ~50 rows of data into a textarea which looks something like this:

aosijpoaspo4koas4
aso4kaps4kaso4pas
4asp4sak,as4aposk4paos
paosk4poas,asas4as4aspkoå4as
as4kopasap sk4poask45 4k4 4 

4 rkop43kr p4

¤p 43kpo4 kp¤PO k4 
4pw ok4
,4ok po4w ,
 4opkrpo4rp4o

2. I want to remove all lines that does NOT contain a comma, this should be fairly easy by first exploding the string with \n as separator and make it become and array (which I have done in my code)

 

3. Then I want to print out all the lines which has a comma.

 

I am stuck here and my head spins a lot because of all this thinking. Does it mean I am stupid? This is very hard for me. :(

 

 

First problem is that I can't check the length of an array with strlen so that's where I am currently stuck...

                                                if(isset($_POST['usernames'])) {
					  	
						  	$str = $_POST['usernames'];

						  	//Explode $str to an array, each line separated by line breaks \n
							$arr = explode("\n", $str);


							//If row contains "," add it to an array and print it
							for ($x = 0; $x < strlen($arr); $x++) {
								if (preg_match('/,/',$arr[$x])) {
									    echo $arr[$x] . "<br>";
								}
							}
													
							    					
					  
						}

Yep I got a spelling error in the title but I can't change it...

Edited by Notreallybrad
Link to comment
Share on other sites

To loop through the array, you could use a foreach loop:

http://php.net/manual/en/control-structures.foreach.php

 

That way you wouldn't need to worry about a counter. But if you need the counter, for whatever reason, you could use the count() function.

http://php.net/manual/en/function.count.php

 

To determine if a line contains a comma, you could avoid regular expressions by using strpos() instead.

http://php.net/manual/en/function.strpos.php

 

Note that you'll want to read the warning under the Return Values section.

Link to comment
Share on other sites

Basically, you could do something like this

//Explode $str to an array, each line separated by line breaks \n
$arr = explode("\n", $str);
$values_withCommas = array();
 
//If row contains "," add it to an array and print it
foreach ($arr as $currLine) {
    if (strpos($currLine, ',') !== false) {
        $values_withCommas[] = $currLine;
    }
}
 
//Display result
echo implode('<br>', $values_withCommas);
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.