vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vB3 Programming Discussions (https://vborg.vbsupport.ru/forumdisplay.php?f=15)
-   -   PHP: how to find a line feed in a text (https://vborg.vbsupport.ru/showthread.php?t=108628)

Arjan 02-22-2006 02:59 PM

PHP: how to find a line feed in a text
 
I have a text in $mytext

Now I want to cut that text after the 5th line.
Doing so by estimating that every line is, say 100 chars, doesn't allways work.

Like in this case where every line has a line feed:
Line 1
Line 2
Line 3
Line 4
Line 5
Line 6
Line 7

How can I detect (and count) the line feeds in this text and determine which one is the 5th, so I know where to cut my text?

Code Monkey 02-23-2006 01:27 AM

PHP Code:

$mytext = <<<HDOC
Line 1
Line 2
Line 3
Line 4
Line 5
Line 6
Line 7
HDOC;
$temp_array explode("\n"$mytext); 

Eplode the page into an array using the newlines.

Then of course, line five is
PHP Code:

echo $temp_array[4]; 

You can also loop through it and delete lines before or after line five. You didn't specify.

PHP Code:

$line_count count($temp_array);
for(
$i 0$i $line_count$i++)
{
    if(
$i == 4)
    {
        echo 
$temp_array[$i];
    }


PHP Code:

$line_count count($temp_array);
$new_text '';
for(
$i 5$i $line_count$i++)
{
    
$new_text .= $temp_array[$i]."\n";
}
echo 
$new_text

PHP Code:

$line_count count($temp_array);
$new_text '';
for(
$i 0$i 5$i++)
{
    
$new_text .= $temp_array[$i]."\n";
}
echo 
$new_text


Marco van Herwaarden 02-23-2006 08:17 AM

Tip: also have a look at array_slice()

Arjan 02-23-2006 01:21 PM

Thanks, that should help me out.


All times are GMT. The time now is 02:17 AM.

Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.

X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.02066 seconds
  • Memory Usage 1,721KB
  • Queries Executed 10 (?)
More Information
Template Usage:
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (5)bbcode_php_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (4)printthreadbit
  • (1)spacer_close
  • (1)spacer_open 

Phrase Groups Available:
  • global
  • postbit
  • showthread
Included Files:
  • ./printthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/class_bbcode_alt.php
  • ./includes/class_bbcode.php
  • ./includes/functions_bigthree.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • printthread_start
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete