Jump to content

Recommended Posts

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 a
Character b
Character d
Character e
Character f
Character h
Character i
Character k
Character l
Character n
Character o
Character p
Character r
Character s
Character t
Character u
Character y

As you can see, the first two are spaces. So i am guessing the first one is regular space (&nbsp) 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++;
}

Link to comment
https://forums.phpfreaks.com/topic/13803-finding-n/
Share on other sites

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
Link to comment
https://forums.phpfreaks.com/topic/13803-finding-n/#findComment-53651
Share on other sites

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...
Link to comment
https://forums.phpfreaks.com/topic/13803-finding-n/#findComment-53679
Share on other sites

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
Link to comment
https://forums.phpfreaks.com/topic/13803-finding-n/#findComment-53680
Share on other sites

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.
Link to comment
https://forums.phpfreaks.com/topic/13803-finding-n/#findComment-53681
Share on other sites

[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
Link to comment
https://forums.phpfreaks.com/topic/13803-finding-n/#findComment-53688
Share on other sites

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
Link to comment
https://forums.phpfreaks.com/topic/13803-finding-n/#findComment-53694
Share on other sites

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.
Link to comment
https://forums.phpfreaks.com/topic/13803-finding-n/#findComment-53698
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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