assigning html code to a variable

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
vpendse
Forum Newbie
Posts: 3
Joined: Wed Apr 12, 2006 10:50 pm

assigning html code to a variable

Post by vpendse »

hi, I'm pretty new to php, and this is probably a simple question. what I'm trying to do is manipulate source code of a website to display only the part of the code I want it to display. so I'm planning to assign the code of a website to a variable, then play around with that variable until it only shows what I want. I know there is a way to assign the results/source code of a website to a variable, I just forget how. can someone help me out! thanks.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I certainly hope you have permission to do this.

Anyways, depending on settings, file_get_contents() can request a page, so can file(), curl and fsockopen(), among other things.
printf
Forum Contributor
Posts: 173
Joined: Wed Jan 12, 2005 5:24 pm

Post by printf »

There are all kinds of methods to do this...

You can read from a file...

Code: Select all

//put the contents of some_file.txt in the variable $data

$data = fread ( fopen ( 'some_file.txt', 'rb' ), filesize ( 'some_file.txt' ) );

echo $data;
Another file reading method...

Code: Select all

//put the contents of local_file.txt in the variable $data

$data = file_get_contents ( './local_file.txt' ); // can fetch URL(s) to

echo $data;

How about putting the file, into array, each element is a single line terminated by \r?\n

Code: Select all

//put the contents of local_file.txt in the variable $data = array ();

$data = file ( 'local_file.txt' );

print_r ( $data );

Have a look at PHP FILE FUNCTIONS, they should help you do what you want!

pif!
vpendse
Forum Newbie
Posts: 3
Joined: Wed Apr 12, 2006 10:50 pm

Post by vpendse »

Thanks for the quick responses! They certainly helped. Is there a way in PHP to search for the first occurence of a string of text in a variable and truncate to that position?
vpendse
Forum Newbie
Posts: 3
Joined: Wed Apr 12, 2006 10:50 pm

Post by vpendse »

I found strstr() ! Hooray.
Post Reply