Jump to content


Photo

Finding \n


  • Please log in to reply
9 replies to this topic

#1 play_

play_
  • Staff Alumni
  • Advanced Member
  • 717 posts

Posted 06 July 2006 - 12:43 AM

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:
$text = row['news'];
$chart = count_chars($text, 1);
   foreach($chart as $letter=>$frequency) {
	echo "Character ".chr($letter). "<br />";
  }

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


regex.kat.sh --- regex library
u.kat.sh ---- url shortener
tabbit.org ---- tabbed pastebin

#2 play_

play_
  • Staff Alumni
  • Advanced Member
  • 717 posts

Posted 06 July 2006 - 02:03 AM

Ok i got it.
here's what i wanted to do:

		$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>';

Here's what it does:
Posted Image

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
regex.kat.sh --- regex library
u.kat.sh ---- url shortener
tabbit.org ---- tabbed pastebin

#3 play_

play_
  • Staff Alumni
  • Advanced Member
  • 717 posts

Posted 06 July 2006 - 04:12 AM

back. It doesn't work quite as expected. I am using highlight_string(), here's whats happening.

this script:
$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>';
outputs this:
Posted Image



So what is a fix to this?
I need to find \n instead of <br />'s...
regex.kat.sh --- regex library
u.kat.sh ---- url shortener
tabbit.org ---- tabbed pastebin

#4 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 06 July 2006 - 04:18 AM

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

#5 play_

play_
  • Staff Alumni
  • Advanced Member
  • 717 posts

Posted 06 July 2006 - 04:20 AM

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.
regex.kat.sh --- regex library
u.kat.sh ---- url shortener
tabbit.org ---- tabbed pastebin

#6 play_

play_
  • Staff Alumni
  • Advanced Member
  • 717 posts

Posted 06 July 2006 - 04:23 AM

Also, i tried what you said, putting double quotes around \n.

it ALMOST worked, here's the result
Posted Image

the last few lines aren't showing numbers.
regex.kat.sh --- regex library
u.kat.sh ---- url shortener
tabbit.org ---- tabbed pastebin

#7 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 06 July 2006 - 04:28 AM

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?
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#8 play_

play_
  • Staff Alumni
  • Advanced Member
  • 717 posts

Posted 06 July 2006 - 04:37 AM

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?


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
regex.kat.sh --- regex library
u.kat.sh ---- url shortener
tabbit.org ---- tabbed pastebin

#9 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 06 July 2006 - 04:50 AM

Here's an easier way of doing this.
<?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";
?>

I believe this will do what you want (untested)

Ken

#10 play_

play_
  • Staff Alumni
  • Advanced Member
  • 717 posts

Posted 06 July 2006 - 05:02 AM

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.
regex.kat.sh --- regex library
u.kat.sh ---- url shortener
tabbit.org ---- tabbed pastebin




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users