Jump to content

Archived

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

play_

Finding \n

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++;
}

Share this post


Link to post
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

Share this post


Link to post
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...

Share this post


Link to post
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

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
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

Share this post


Link to post
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

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

×

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.