...

View Full Version : Removing the last and first line of a variable..



sir pannels
05-03-2004, 11:07 PM
Urg yep me again - ran into yet another problem...

I have a variable (some of you may know what project im referring to here) that has many lines of writing in.. i need to remove the first line and the last line...

Now.. im pretty sure im going to have to use an array for this? which is just my fav thing :/.. I studied Perl for 3 years and still didnt like using arrays :| so when it comes to php im even further away
but anyway I still had a go tho without using first and last line of the var as I dont know how to get those things, is there a function for that ?.. but here is my attempt at the array stuff.. "pretending" first line is fLine and last line is lLine..



<?
$myary = $fLine."/".$lLine.;
$pdt = explode("/",$myary);

echo "after: ";
foreach($pdt as $value)
{
echo $value." ";
}



but then reliased this will only give me the 2 lines I dont want ?!
So as you can see - im totally lost with this.. any help would be great..

Thanks very much.
Sir P

raf
05-03-2004, 11:23 PM
$var='line1/xllllllx/line2';
$var=substr($var, (strpos($var, '/')+1));
$var=substr($var, 0, strrpos($var, '/'));

<edit>needs to be done in 2 steps because the strrpos else takes the initial first character as base for the position of /</edit>

sir pannels
05-03-2004, 11:44 PM
hey raf.. that returns
xllllllx
and only that.. is that a fault of my own? im calling it with $var.

cheers
Sir P

raf
05-03-2004, 11:57 PM
hey raf.. that returns
xllllllx
and only that.. is that a fault of my own? im calling it with $var.

cheers
Sir P
I thought your lines were seperated by '/', so the code i posted will remove everything until the first / and everything behind the last /

If your lines are seperated with a diferent character, then you need to replace the / inside the code by that seperating character.

$var is just a dummy variablename here, but it can be anything of course.

sir pannels
05-04-2004, 12:05 AM
ah yeh i see- due to my try where i used examples..

well they are seperate by a line break.. but not.. <br> as its not html.. do i need to find and use the ascii code for that?

raf
05-04-2004, 12:22 AM
try

$var=substr($var, (strpos($var, "\n")+1));
$var=substr($var, 0, strrpos($var, "\n"));

firepages
05-04-2004, 03:22 AM
well rafs is cleaner & probably quicker .... but since you like arrays so much ;) a bit of array-popping etc



<?
$str='remove this
but not this
and definately not this
snarf - blurgh
errr this can go as well';

/*make an array*/
$bits = explode("\n",$str);

/*remove the first item*/
array_shift($bits);

/*remove the last item*/
array_pop($bits);

/*rebuild it*/
echo implode($bits);
?>

sir pannels
05-04-2004, 09:34 AM
thats great... i tested both.. and they do both work great - tho due to my fear of array's im gonig with raf's... but ive saved the array under a comment in the code for reference:)

Last question.. would what im using here.. rafs code.. how hard would it be to get it to remove everything, no matter what line number before the first apperance of "whatever" ?


Thank you .. heros ;)

raf
05-04-2004, 10:24 AM
The array-approach is probably slower and more resource eating anyway (i think :D )

Last question.. would what im using here.. rafs code.. how hard would it be to get it to remove everything, no matter what line number before the first apperance of "whatever" ?

Hmm. Seems like you're not realy inderstanding the code.
strpos($var, "\n") returns the position of the first occurence of \n inside the string?
substr returns the part of the string, from the value from the second argument on, until the value of the third argument.
substr($var, 0, 10) returns the first 10 characters
substr($var, 10) return the string from the 11 position on, till the end of the string.
So
$var=substr($var, (strpos($var, "\n")+1));
returns the substring from the first occurence of "\n" + 1

If you replace the "\n" by 'test' then you'll the string from right after 'test' till the end of the string

sir pannels
05-04-2004, 10:33 AM
ohhhhh.... *clicks*

I get it...it collects the string from whatever character(s) you have in here
$var=substr($var, (strpos($var, "\n")+1));

Makes perfect sense now... got it down:)...
Thanks very much raf - been a great help, again ;)


Sir P

raf
05-04-2004, 10:47 AM
You're welcome. Happy coding !

firepages
05-04-2004, 11:29 AM
The array-approach is probably slower and more resource eating anyway

that was my assumption as well , & I just did a few tests & whilst for small strings arrays are no slower ...for larger ones (& not very large either) it slows down considerably, over twice as slow as rafs method :cool:

sir pannels
05-04-2004, 11:38 AM
no bloodloose then?! :D

raf
05-04-2004, 12:20 PM
no bloodloose then?! :D
Hmm. As long as you don't master array, your just a wannabe and we will keep laughing behind your back :D

nice going firepages :thumbsup: (i was to lazy for that).



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum