Page 1 of 1

assigning html code to a variable

Posted: Wed Apr 12, 2006 10:53 pm
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.

Posted: Wed Apr 12, 2006 11:16 pm
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.

Posted: Wed Apr 12, 2006 11:20 pm
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!

Posted: Wed Apr 12, 2006 11:36 pm
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?

Posted: Wed Apr 12, 2006 11:37 pm
by vpendse
I found strstr() ! Hooray.