Jump to content

read first to lines of content


scarhand

Recommended Posts

lets say i have content that is formatted like this in my database:

 

line 1 value
line 2 value

this is some content this is some content this is some content this is some content this is some content this is some content this is some content this is some content this is some content this is some content this is some content this is some content this is some content this is some content this is some content this is some content this is some content this is some content this is some content this is some content this is some content this is some content this is some content this is some content this is some content this is some content

 

how would i get the line 1 and line 2 value?

Link to comment
https://forums.phpfreaks.com/topic/120522-read-first-to-lines-of-content/
Share on other sites

with str_pos() find the new lines position : 

$content = line 1 value
line 2 value

this is some content this is some content this is ...


$pos = str_pos($content,"\n");

 

then with substr()  get  it : 

substr($content, 0, $pos);

 

 

then go for second line : 

 

$second =  substr($content ,  $pos);

$pos2 =  str_pos($second,"\n");

substr($second, 0, $pos2);

 

 

 

 

 

well i had to replace str_pos with strpos

 

and i got the first line to work

 

however, the second line is not working

 

heres my code:

 

<?php
    $pos = strpos($content,"\n");
    $one = substr($content, 0, $pos);
    
    $second = substr($content, $pos);
    $pos2 = strpos($second,"\n");
    $two = substr($second, 0, $pos2);

when you echo the $two what you get ? 

 

probably you gotta do this  :

 

    $second = substr($content, $pos+1);  // notice at +1 
    $pos2 = strpos($second,"\n");
    $two = substr($second, 0, $pos2);

 

cause it passes the new line and starts from the first of the second line.

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.