Jump to content

Counting SPECIFIC Lines inside Textarea


UpcomingPhpDev

Recommended Posts

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

<?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

<?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";
  
?>

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";
   
?>

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";
   
?>

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.