play_ Posted July 6, 2006 Share Posted July 6, 2006 Hi!I pulled an entry from the database, and i need to count how many \n or <br />'s there are.I've tried using this:[code]$text = row['news'];$chart = count_chars($text, 1); foreach($chart as $letter=>$frequency) { echo "Character ".chr($letter). "<br />"; }[/code]Jus to display each character. I get this result:Character Character Character Character aCharacter bCharacter dCharacter eCharacter fCharacter hCharacter iCharacter kCharacter lCharacter nCharacter oCharacter pCharacter rCharacter sCharacter tCharacter uCharacter yAs you can see, the first two are spaces. So i am guessing the first one is regular space ( ) and the other one is a new line or <br />.Kinda lost where to go from here. :(so let's say i wanna do a loop of how many times <br /> or \n was found. $i = 0;loop { if found, $i++;} Quote Link to comment https://forums.phpfreaks.com/topic/13803-finding-n/ Share on other sites More sharing options...
play_ Posted July 6, 2006 Author Share Posted July 6, 2006 Ok i got it.here's what i wanted to do:[code] $text = nl2br($news); $words_to_count = array('<br />'); echo' <table border=1 width=100%> <tr> <td valign=top>'; $i = 1; foreach($words_to_count as $words) { $number = substr_count($text, $words); while ($i <= $number + 1) { echo $i++ . '<br />'; } } echo ' </td> <td>' . $text . '</td> </tr> </table>';[/code]Here's what it does:[img]http://img.photobucket.com/albums/v425/play/help/fff97c1b.jpg[/img]It adds a number to the left after each line.Perfect if you plan on posting a script of yours on your site.However, if anyone knows of a simpler way of doing this, let me know.Thanks Quote Link to comment https://forums.phpfreaks.com/topic/13803-finding-n/#findComment-53651 Share on other sites More sharing options...
play_ Posted July 6, 2006 Author Share Posted July 6, 2006 back. It doesn't work quite as expected. I am using highlight_string(), here's whats happening.this script:[code]$text = $news; $words_to_count = array('\n'); echo' <table border=1 width=100%> <tr> <td valign=top>'; $found = 1; foreach($words_to_count as $words) { // the $number + 1 part is so the numbers on the left align the lines on the right. // if it wasn't there, there would be one more line than a number. // take it out and see what happens. $number = substr_count($text, $words); // number of times $words(<br />) occurs in $text (text pulled from db). while ($found <= $number + 1) { echo $found++ . '<br />'; } } echo ' </td> <td>' . highlight_string($text,1) . '</td> </tr> </table>';[/code]outputs this:[img]http://img.photobucket.com/albums/v425/play/help/f187b70d.jpg[/img]So what is a fix to this?I need to find \n instead of <br />'s... Quote Link to comment https://forums.phpfreaks.com/topic/13803-finding-n/#findComment-53679 Share on other sites More sharing options...
kenrbnsn Posted July 6, 2006 Share Posted July 6, 2006 If you want to search for a newline character "\n", you need to enclose it in double quotes or your will just be searching for the string '\n'. These are two different strings.Can you explain again exact what you're trying to accomplish with this code.Ken Quote Link to comment https://forums.phpfreaks.com/topic/13803-finding-n/#findComment-53680 Share on other sites More sharing options...
play_ Posted July 6, 2006 Author Share Posted July 6, 2006 Hello Ken,On my site, i plan on posting the sourcecode of some scripts i have made.I have a form that sends it right to the database.When retrieved from the database, i'd like to have it formatted like in the pictures above.the number of lines on the left, and the script on the right. Quote Link to comment https://forums.phpfreaks.com/topic/13803-finding-n/#findComment-53681 Share on other sites More sharing options...
play_ Posted July 6, 2006 Author Share Posted July 6, 2006 Also, i tried what you said, putting double quotes around \n.it ALMOST worked, here's the result[img]http://img.photobucket.com/albums/v425/play/help/b84005ba.jpg[/img]the last few lines aren't showing numbers. Quote Link to comment https://forums.phpfreaks.com/topic/13803-finding-n/#findComment-53683 Share on other sites More sharing options...
.josh Posted July 6, 2006 Share Posted July 6, 2006 i think because here:$number = substr_count($text, $words); // number of times $words(<br />) occurs in $text (text pulled from d...$number isn't counting your \n's? Quote Link to comment https://forums.phpfreaks.com/topic/13803-finding-n/#findComment-53685 Share on other sites More sharing options...
play_ Posted July 6, 2006 Author Share Posted July 6, 2006 [quote author=Crayon Violent link=topic=99597.msg392310#msg392310 date=1152160131]i think because here:$number = substr_count($text, $words); // number of times $words(<br />) occurs in $text (text pulled from d...$number isn't counting your \n's?[/quote]it because when it outputs, the text is longer than the <div> holding it, therefore it creates a new line on the fly, adding on to the ones being pulled from the database.Im gonna try to find a fix for this, if anyone knows, let me know. =D Quote Link to comment https://forums.phpfreaks.com/topic/13803-finding-n/#findComment-53688 Share on other sites More sharing options...
kenrbnsn Posted July 6, 2006 Share Posted July 6, 2006 Here's an easier way of doing this.[code]<?php//// assume the script you want to display is in $row['script']//$tmp1 = array();$tmp = explode("\n",$row['script']); // explode the stored script on the newline character creating the array $tmp$n = 1;$tmp1[] = '<table>';foreach($tmp as $line) { $tmp1[] = '<tr><td>' . $n . '</td><td>' . highlight_string($line,true) . '</td></tr>'; $n++;}$tmp1[] = '</table>';echo implode("\n",$tmp1)."\n";?>[/code]I believe this will do what you want (untested)Ken Quote Link to comment https://forums.phpfreaks.com/topic/13803-finding-n/#findComment-53694 Share on other sites More sharing options...
play_ Posted July 6, 2006 Author Share Posted July 6, 2006 Yes Ken, Thank you.The only thing is, when you hightlite the code with the mouse, the numbers on the left get highlited too.So, since i am almost done with my version of this, ill try to finish it (plus, feels better when you make something 100% yourself you know). But if i can't, then i will use your version. Indeed it is smaller and simpler. Thanks again ken.::edit::Yea, ill use this with some modifications.Thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/13803-finding-n/#findComment-53698 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.