Jump to content

[SOLVED] Counting How Many Lines Are Inside Certain Markup Tags.


Vermillion

Recommended Posts

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?

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]

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 ;_;.

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?

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]

  • 2 weeks later...

<?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


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];

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
                )

        )

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)

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.

Archived

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

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