UpcomingPhpDev Posted July 17, 2008 Share Posted July 17, 2008 Hi all, My first post here, Basically im having some trouble with counting specific lines inside a textarea,What I want to acheive is to count only the lines in a textarea that start with http, I have tried so many different ways but they always end up counting every line inside the textarea, Can you help me please? Thx in advance. <?php $Textarea = $_POST['textarea']; if(!empty($Textarea)) { $Lines= nl2br($Textarea) } ?> Link to comment https://forums.phpfreaks.com/topic/115315-counting-specific-lines-inside-textarea/ Share on other sites More sharing options...
gijew Posted July 17, 2008 Share Posted July 17, 2008 <?php $textarea = explode("\n", $Textarea); $total = count($textarea); ?> HTH Link to comment https://forums.phpfreaks.com/topic/115315-counting-specific-lines-inside-textarea/#findComment-592850 Share on other sites More sharing options...
UpcomingPhpDev Posted July 17, 2008 Author Share Posted July 17, 2008 <?php $textarea = explode("\n", $Textarea); $total = count($textarea); ?> HTH I knew atleast someone wouldnt read the question properly, which is why i even put SPECIFIC in capitals, I wish to only count the lines that start with http. Thx for your answer but I already knew this Link to comment https://forums.phpfreaks.com/topic/115315-counting-specific-lines-inside-textarea/#findComment-592853 Share on other sites More sharing options...
trq Posted July 17, 2008 Share Posted July 17, 2008 <?php <?php $count = 0; $textarea = $_POST['textarea']; $lines = explode("\n",$textarea); // if this doesn't work try using \n\r instead of \n foreach ($lines as $line) { if (substr($line,3) == 'http') { $count++; } } echo "There are $count lines beggining with http"; ?> Link to comment https://forums.phpfreaks.com/topic/115315-counting-specific-lines-inside-textarea/#findComment-592858 Share on other sites More sharing options...
UpcomingPhpDev Posted July 17, 2008 Author Share Posted July 17, 2008 Hello thorpe, I wrote a few similar scripts like this before I posted here, I have been trying yours since you posted it but it doesnt seem to work, Did you test it(did it work for you?). Ive tried changing \n to <br /> etc but it still doesnt work. thanks for the help <form> <textarea name="textarea" rows="10" cols="20"></textarea> <input type="submit" value="Convert"> </form> <?php $Textarea = $_POST['textarea']; $Lines = nl2br($Textarea); $count = 0; $lines = explode("\n",$Lines); // if this doesn't work try using \n\r instead of \n foreach ($lines as $line) { if (substr($line,3) == 'http') { $count++; echo $count; } } echo "There are $count lines beggining with http"; ?> Link to comment https://forums.phpfreaks.com/topic/115315-counting-specific-lines-inside-textarea/#findComment-592873 Share on other sites More sharing options...
trq Posted July 17, 2008 Share Posted July 17, 2008 Sorry, im not watching what I'm doing and no, I never test code. <?php $textarea = $_POST['textarea']; $count = 0; $lines = explode("\n",$textarea); // if this doesn't work try using \n\r instead of \n foreach ($lines as $line) { if (substr($line,0,4) == 'http') { $count++; echo $count; } } echo "There are $count lines beggining with http"; ?> Link to comment https://forums.phpfreaks.com/topic/115315-counting-specific-lines-inside-textarea/#findComment-592882 Share on other sites More sharing options...
UpcomingPhpDev Posted July 17, 2008 Author Share Posted July 17, 2008 Sorry, im not watching what I'm doing and no, I never test code. <?php $textarea = $_POST['textarea']; $count = 0; $lines = explode("\n",$textarea); // if this doesn't work try using \n\r instead of \n foreach ($lines as $line) { if (substr($line,0,4) == 'http') { $count++; echo $count; } } echo "There are $count lines beggining with http"; ?> Arr, even I should have noticed to add 4 to the substr so that it starts from position 0 and counts to 4 characters, Thx for the help thorpe. Also thx for the fast help Link to comment https://forums.phpfreaks.com/topic/115315-counting-specific-lines-inside-textarea/#findComment-592886 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.