Notreallybrad Posted September 9, 2016 Share Posted September 9, 2016 (edited) 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 September 9, 2016 by Notreallybrad Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted September 9, 2016 Share Posted September 9, 2016 Um, what? Where does this bizarre input come from? Why do some rows have commas and others not? Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted September 9, 2016 Share Posted September 9, 2016 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. Quote Link to comment Share on other sites More sharing options...
requinix Posted September 9, 2016 Share Posted September 9, 2016 Yep I got a spelling error in the title but I can't change it...But I can! Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted September 9, 2016 Share Posted September 9, 2016 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); Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.