Vermillion Posted March 2, 2009 Share Posted March 2, 2009 When the user submits content inside the [code][/code] tags of my site, I want to count how many lines of text are inside the tags. For example: [code] <a href="lol.php>Inside</a> <br /> <strong>Go to lol site</strong> [/code] There are three lines of text inside the code tags. How can I make PHP tell me that? Quote Link to comment https://forums.phpfreaks.com/topic/147510-solved-counting-how-many-lines-are-inside-certain-markup-tags/ Share on other sites More sharing options...
RussellReal Posted March 2, 2009 Share Posted March 2, 2009 explode by \n? Quote Link to comment https://forums.phpfreaks.com/topic/147510-solved-counting-how-many-lines-are-inside-certain-markup-tags/#findComment-774337 Share on other sites More sharing options...
Vermillion Posted March 2, 2009 Author Share Posted March 2, 2009 How would I explode everything inside the code tags, then? I don't want to explode the whole string, because the users may submit other tags that are outside the Code tags. Quote Link to comment https://forums.phpfreaks.com/topic/147510-solved-counting-how-many-lines-are-inside-certain-markup-tags/#findComment-774343 Share on other sites More sharing options...
RussellReal Posted March 2, 2009 Share Posted March 2, 2009 u c.. thats where you'd use regex or strpos && substr preg_match strpos && substr Quote Link to comment https://forums.phpfreaks.com/topic/147510-solved-counting-how-many-lines-are-inside-certain-markup-tags/#findComment-774348 Share on other sites More sharing options...
Vermillion Posted March 2, 2009 Author Share Posted March 2, 2009 Okay, been trying to figure out how I would use them all, but I still can't find my answer. Any other help on how exactly should I used them? I will really appreciate it. Quote Link to comment https://forums.phpfreaks.com/topic/147510-solved-counting-how-many-lines-are-inside-certain-markup-tags/#findComment-774356 Share on other sites More sharing options...
xerodefect Posted March 2, 2009 Share Posted March 2, 2009 Okay, been trying to figure out how I would use them all, but I still can't find my answer. Any other help on how exactly should I used them? I will really appreciate it. Here try this little function I just wrote for you. <?php $string = "Blah blah hello world =) [code]Code for you? skip a few ahah testing hehe one line "; function countLines($String = "") { $lines = array(); if( preg_match_all('#\ [code\]([^]]+)\[/code\]#i', $String, $Matchs, PREG_SET_ORDER)){ foreach($Matchs AS $Match) { $test = explode("\r\n", $Match[0]); if($test[0] == ' ') { array_shift($test); } if($test[count($test)-1] == ' ') { array_pop($test); } $lines[] = count($test); } } return $lines; } var_dump(countLines($string)); ?> [/code] Quote Link to comment https://forums.phpfreaks.com/topic/147510-solved-counting-how-many-lines-are-inside-certain-markup-tags/#findComment-774358 Share on other sites More sharing options...
Vermillion Posted March 2, 2009 Author Share Posted March 2, 2009 Thanks for all the help, now thought, the code is quite abstract for me: why are you using foreach? I though it was just used for arrays, and I don't see anything with arrays there, I see just see them after the foreach loop ;_;. Quote Link to comment https://forums.phpfreaks.com/topic/147510-solved-counting-how-many-lines-are-inside-certain-markup-tags/#findComment-774381 Share on other sites More sharing options...
RussellReal Posted March 2, 2009 Share Posted March 2, 2009 in preg match all... it fills that variable with an array Quote Link to comment https://forums.phpfreaks.com/topic/147510-solved-counting-how-many-lines-are-inside-certain-markup-tags/#findComment-774388 Share on other sites More sharing options...
Vermillion Posted March 2, 2009 Author Share Posted March 2, 2009 Hm, the function is not doing what I wanted. When I echoed it just printed "Array". I want it to actually return an integer value. When I use var_dump I see the whole array, and that really wont help me for what I want to do. Any modifications I can do to make sure it returns just an integer? Quote Link to comment https://forums.phpfreaks.com/topic/147510-solved-counting-how-many-lines-are-inside-certain-markup-tags/#findComment-774409 Share on other sites More sharing options...
Philip Posted March 2, 2009 Share Posted March 2, 2009 It's because it returns an array item for each instance of the blocks. So $result = countLines($string); if(isset($result[0]) echo $result[0]; // the above line will show how many lines in the first [code] block if(isset($result[1]) echo $result[1]; // this one shows the second, if it exists... etc.[/code] Quote Link to comment https://forums.phpfreaks.com/topic/147510-solved-counting-how-many-lines-are-inside-certain-markup-tags/#findComment-774422 Share on other sites More sharing options...
Vermillion Posted March 2, 2009 Author Share Posted March 2, 2009 Ah okay, I see that now. Thanks a lot. Now, my last request. Can someone comment/document that code? I am having a very difficult time trying to understand it completely ;_;. Quote Link to comment https://forums.phpfreaks.com/topic/147510-solved-counting-how-many-lines-are-inside-certain-markup-tags/#findComment-774430 Share on other sites More sharing options...
Vermillion Posted March 13, 2009 Author Share Posted March 13, 2009 My apologies for the bump and double post, but can anyone explain the code to me? Being trying to figure out how it works for about 2 weeks now, with no avail. Quote Link to comment https://forums.phpfreaks.com/topic/147510-solved-counting-how-many-lines-are-inside-certain-markup-tags/#findComment-783523 Share on other sites More sharing options...
Philip Posted March 13, 2009 Share Posted March 13, 2009 <?php // create a test string: $string = "Blah blah hello world =) [code]Code for you? skip a few ahah testing hehe one line "; // a new function, with 1 parameter that is required // note, i did change this so the string was required unlike the original function countLines($String) { // setup a new array, dont put anything in it yet $lines = array(); // if it successful in finding code tags in the string if( preg_match_all('#\ [code\]([^]]+)\[/code\]#i', $String, $Matchs, PREG_SET_ORDER)){ // loop through each match foreach($Matchs AS $Match) { // create a temporary array, for each line of the code tag // (when there are multiple lines inside ) $test = explode("\r\n", $Match[0]); // if the first line is just , we should get rid of it if($test[0] == '[code]') { array_shift($test); // (shifts array, so it'll ignore the [code] } // if the last line is , we need to "pop" it off the array if($test[count($test)-1] == '[/code]') { array_pop($test); // pretty much deletes the array item ([/code] } // count how many array items (or lines) are in the array still $lines[] = count($test); } } // return the array of the line counts for each block return $lines; } // show an example of what it looks like and what values to call var_dump(countLines($string)); ?>[/code] Does that help? --oops, looks like it didnt like it in code tags, we'll have to use Quote Link to comment https://forums.phpfreaks.com/topic/147510-solved-counting-how-many-lines-are-inside-certain-markup-tags/#findComment-783525 Share on other sites More sharing options...
Vermillion Posted March 14, 2009 Author Share Posted March 14, 2009 Okay, thanks a lot for the help so far. Now, I need to do a small improvement to this code. I need to store the whole blocks with PHP tags, but only the blocks, so if people put this: The HTML code to lol is this [code] lol [/code] and the code to fix that is this: [code] fixed lol [/code] So in the function I would store "[code]lol [/code]" and "[code]fixed lol [/code]" in array elements, so I can do something like this: $codes = array(); //The array would have something like: $codes[0] = "[c0de]lol[/c0de]"; $codes[1] = "[c0de]fixed lol[/c0de]"; //so I could eco something like: echo "the first code block (".$codes[0].") has " .$lines[0]; Quote Link to comment https://forums.phpfreaks.com/topic/147510-solved-counting-how-many-lines-are-inside-certain-markup-tags/#findComment-784727 Share on other sites More sharing options...
Philip Posted March 15, 2009 Share Posted March 15, 2009 Just call $Matchs[0], $Matchs[1] etc.... it'll still show the though. Quote Link to comment https://forums.phpfreaks.com/topic/147510-solved-counting-how-many-lines-are-inside-certain-markup-tags/#findComment-784978 Share on other sites More sharing options...
Vermillion Posted March 15, 2009 Author Share Posted March 15, 2009 How can I actually do that? I am already returning the $string, I don't know how I can return or call the $Matchs afterwards . Quote Link to comment https://forums.phpfreaks.com/topic/147510-solved-counting-how-many-lines-are-inside-certain-markup-tags/#findComment-785332 Share on other sites More sharing options...
Philip Posted March 15, 2009 Share Posted March 15, 2009 Here we go: <?php error_reporting(E_ALL); // create a test string: $string = "Blah blah hello world =) [code]Code for you? skip a few ahah testing hehe one line "; // a new function, with 1 parameter that is required // note, i did change this so the string was required unlike the original function countLines($String) { // setup a new array, dont put anything in it yet $lines = array(); // if it successful in finding code tags in the string if( preg_match_all('#\ [code\]([^]]+)\[/code\]#i', $String, $Matches, PREG_SET_ORDER)){ // get line count $lineCount = count($Matches); // setup an array $returnArray = array(); // loop through each match for($i=0; $i<$lineCount; $i++) { // create a temporary array, for each line of the code tag // (when there are multiple lines inside ) $test = explode("\r\n", $Matchs[$i][0]); // if the first line is just , we should get rid of it if($test[0] == '[code]') { array_shift($test); // (shifts array, so it'll ignore the [code] } // if the last line is , we need to "pop" it off the array if($test[count($test)-1] == '[/code]') { array_pop($test); // pretty much deletes the array item ([/code] } // count how many array items (or lines) are in the array still $returnArray[$i]['num'] = count($test); // and give the data back: $returnArray[$i]['data'] = preg_replace(array('~\ [code\]~','~\[/code\]~'), '',$test); } } // return the array of the line counts for each block return $returnArray; } // show an example of what it looks like and what values to call echo '<pre>'; print_r(countLines($string)); echo '</pre>'; ?>[/code] That'll show: Array ( [0] => Array ( [num] => 5 [data] => Array ( [0] => Code for you? [1] => [2] => skip a few [3] => ahah [4] => testing ) ) [1] => Array ( [num] => 1 [data] => Array ( [0] => hehe one line ) ) Quote Link to comment https://forums.phpfreaks.com/topic/147510-solved-counting-how-many-lines-are-inside-certain-markup-tags/#findComment-785402 Share on other sites More sharing options...
Philip Posted March 15, 2009 Share Posted March 15, 2009 Oops! Had a typo, fixed version below: <?php error_reporting(E_ALL); // create a test string: $string = "Blah blah hello world =) [code]Code for you? skip a few ahah testing hehe one line "; // a new function, with 1 parameter that is required // note, i did change this so the string was required unlike the original function countLines($String) { // setup a new array, dont put anything in it yet $lines = array(); // if it successful in finding code tags in the string if( preg_match_all('#\ [code\]([^]]+)\[/code\]#i', $String, $Matches, PREG_SET_ORDER)){ // get line count $lineCount = count($Matches); // setup an array $returnArray = array(); // loop through each match for($i=0; $i<$lineCount; $i++) { // create a temporary array, for each line of the code tag // (when there are multiple lines inside ) $test = explode("\r\n", $Matches[$i][0]); // if the first line is just , we should get rid of it if($test[0] == '[code]') { array_shift($test); // (shifts array, so it'll ignore the [code] } // if the last line is , we need to "pop" it off the array if($test[count($test)-1] == '[/code]') { array_pop($test); // pretty much deletes the array item ([/code] } // count how many array items (or lines) are in the array still $returnArray[$i]['num'] = count($test); // and give the data back: $returnArray[$i]['data'] = preg_replace(array('~\ [code\]~','~\[/code\]~'), '',$test); } } // return the array of the line counts for each block return $returnArray; } // show an example of what it looks like and what values to call echo '<pre>'; print_r(countLines($string)); echo '</pre>'; ?>[/code] (missing an 'e' in '$Matches', line 34) Quote Link to comment https://forums.phpfreaks.com/topic/147510-solved-counting-how-many-lines-are-inside-certain-markup-tags/#findComment-785408 Share on other sites More sharing options...
Vermillion Posted March 15, 2009 Author Share Posted March 15, 2009 Haha yeah, I managed to get it though =P! I swear this is my last question >.<: I can't seem to echo the content in 'data' properly. I don't mind if it echoes the [c0de] tags, I will let my markup post to deal with that. I have tried many things, including this (I have renamed the countLines function to codeAmt): $result = codeAmt($string); echo "<pre>"; print_r($result); echo "</pre>"; echo "<br /><br />"; echo $result[0]['num']; echo "<br />"; foreach($result[0]['data'] as $key => $current){ echo $result[0][$current]; } but that is not working, I get this: Notice: Undefined index: in C:\wamp\www\AwingsCLF\lolin1.php on line 217 Notice: Undefined index: Code for you? in C:\wamp\www\AwingsCLF\lolin1.php on line 217 Notice: Undefined index: in C:\wamp\www\AwingsCLF\lolin1.php on line 217 Notice: Undefined index: skip a few in C:\wamp\www\AwingsCLF\lolin1.php on line 217 Notice: Undefined index: ahah in C:\wamp\www\AwingsCLF\lolin1.php on line 217 Notice: Undefined index: testing in C:\wamp\www\AwingsCLF\lolin1.php on line 217 I have even tried everything that even a newbie would do >.>, with no avail. Quote Link to comment https://forums.phpfreaks.com/topic/147510-solved-counting-how-many-lines-are-inside-certain-markup-tags/#findComment-785427 Share on other sites More sharing options...
Philip Posted March 15, 2009 Share Posted March 15, 2009 echo $result[0][$current]; Should be: echo $current; Quote Link to comment https://forums.phpfreaks.com/topic/147510-solved-counting-how-many-lines-are-inside-certain-markup-tags/#findComment-785430 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.