Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 05-03-2004, 11:07 PM   PM User | #1
sir pannels
Regular Coder

 
Join Date: Jun 2002
Posts: 905
Thanks: 23
Thanked 5 Times in 5 Posts
sir pannels is an unknown quantity at this point
Removing the last and first line of a variable..

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

PHP Code:
<?
$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
sir pannels is offline   Reply With Quote
Old 05-03-2004, 11:23 PM   PM User | #2
raf
Master Coder


 
Join Date: Jul 2002
Posts: 6,589
Thanks: 0
Thanked 0 Times in 0 Posts
raf will become famous soon enoughraf will become famous soon enough
$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>
__________________
Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

Last edited by raf; 05-03-2004 at 11:33 PM..
raf is offline   Reply With Quote
Old 05-03-2004, 11:44 PM   PM User | #3
sir pannels
Regular Coder

 
Join Date: Jun 2002
Posts: 905
Thanks: 23
Thanked 5 Times in 5 Posts
sir pannels is an unknown quantity at this point
hey raf.. that returns
xllllllx
and only that.. is that a fault of my own? im calling it with $var.

cheers
Sir P
sir pannels is offline   Reply With Quote
Old 05-03-2004, 11:57 PM   PM User | #4
raf
Master Coder


 
Join Date: Jul 2002
Posts: 6,589
Thanks: 0
Thanked 0 Times in 0 Posts
raf will become famous soon enoughraf will become famous soon enough
Quote:
Originally Posted by sir pannels
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.
__________________
Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html
raf is offline   Reply With Quote
Old 05-04-2004, 12:05 AM   PM User | #5
sir pannels
Regular Coder

 
Join Date: Jun 2002
Posts: 905
Thanks: 23
Thanked 5 Times in 5 Posts
sir pannels is an unknown quantity at this point
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?
sir pannels is offline   Reply With Quote
Old 05-04-2004, 12:22 AM   PM User | #6
raf
Master Coder


 
Join Date: Jul 2002
Posts: 6,589
Thanks: 0
Thanked 0 Times in 0 Posts
raf will become famous soon enoughraf will become famous soon enough
try

$var=substr($var, (strpos($var, "\n")+1));
$var=substr($var, 0, strrpos($var, "\n"));
__________________
Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html
raf is offline   Reply With Quote
Old 05-04-2004, 03:22 AM   PM User | #7
firepages
Super Moderator


 
Join Date: May 2002
Location: Perth Australia
Posts: 3,901
Thanks: 5
Thanked 79 Times in 78 Posts
firepages will become famous soon enough
well rafs is cleaner & probably quicker .... but since you like arrays so much a bit of array-popping etc

PHP Code:
<?
$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);
?>
__________________
resistance is...

MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)
firepages is offline   Reply With Quote
Old 05-04-2004, 09:34 AM   PM User | #8
sir pannels
Regular Coder

 
Join Date: Jun 2002
Posts: 905
Thanks: 23
Thanked 5 Times in 5 Posts
sir pannels is an unknown quantity at this point
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
sir pannels is offline   Reply With Quote
Old 05-04-2004, 10:24 AM   PM User | #9
raf
Master Coder


 
Join Date: Jul 2002
Posts: 6,589
Thanks: 0
Thanked 0 Times in 0 Posts
raf will become famous soon enoughraf will become famous soon enough
The array-approach is probably slower and more resource eating anyway (i think )
Quote:
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
__________________
Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

Last edited by raf; 05-04-2004 at 10:45 AM..
raf is offline   Reply With Quote
Old 05-04-2004, 10:33 AM   PM User | #10
sir pannels
Regular Coder

 
Join Date: Jun 2002
Posts: 905
Thanks: 23
Thanked 5 Times in 5 Posts
sir pannels is an unknown quantity at this point
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
sir pannels is offline   Reply With Quote
Old 05-04-2004, 10:47 AM   PM User | #11
raf
Master Coder


 
Join Date: Jul 2002
Posts: 6,589
Thanks: 0
Thanked 0 Times in 0 Posts
raf will become famous soon enoughraf will become famous soon enough
You're welcome. Happy coding !
__________________
Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html
raf is offline   Reply With Quote
Old 05-04-2004, 11:29 AM   PM User | #12
firepages
Super Moderator


 
Join Date: May 2002
Location: Perth Australia
Posts: 3,901
Thanks: 5
Thanked 79 Times in 78 Posts
firepages will become famous soon enough
Quote:
Originally Posted by raf
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
__________________
resistance is...

MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)
firepages is offline   Reply With Quote
Old 05-04-2004, 11:38 AM   PM User | #13
sir pannels
Regular Coder

 
Join Date: Jun 2002
Posts: 905
Thanks: 23
Thanked 5 Times in 5 Posts
sir pannels is an unknown quantity at this point
no bloodloose then?!
sir pannels is offline   Reply With Quote
Old 05-04-2004, 12:20 PM   PM User | #14
raf
Master Coder


 
Join Date: Jul 2002
Posts: 6,589
Thanks: 0
Thanked 0 Times in 0 Posts
raf will become famous soon enoughraf will become famous soon enough
Quote:
Originally Posted by sir pannels
no bloodloose then?!
Hmm. As long as you don't master array, your just a wannabe and we will keep laughing behind your back

nice going firepages (i was to lazy for that).
__________________
Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html
raf is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:24 PM.


Advertisement
Log in to turn off these ads.