Computing.Net > Forums > Web Development > file_get_content command

file_get_content command

Reply to Message Icon

Original Message
Name: salehramazani
Date: January 3, 2008 at 00:30:57 Pacific
Subject: file_get_content command
OS: W3K
CPU/Ram: 3.2,2gb
Comment:

Someone posted a question here a year ago about this same topic. They had an HTML page and wanted to post a substring (first 50 or so characters) of it on another page. This problem was solved using the php file_get_content command here:
http://www.computing.net/webdevel/w...

I'm trying to do the same thing but to a known portion of the text in middle of my page. In other words, I want to set a start and an end point within my page and grab only a portion of the text that I want like this:

==>Unwanted content

[start wanted content]

==>My content here to appear elsewhere

[end wanted content]

==>Unwanted content

Is there any way to do that?


Report Offensive Message For Removal


Response Number 1
Name: Michael J (by mjdamato)
Date: January 3, 2008 at 07:37:34 Pacific
Subject: file_get_content command
Reply: (edit)

Here's a quick and dirty solution that requires using a couple of "flags" in the source page (you need to replace the [] with <> in the first two lines):

$start_text = "[!--START--]";
$end_text = "[!--END--]";


$start_pos = strpos($start_text, $search) + strlen($search);
$end_pos = strpos($end_text, $search) - $start_pos;


$found_text = substr($source, $start_pos, $end_pos);


Michael J


Report Offensive Follow Up For Removal

Response Number 2
Name: salehramazani
Date: January 4, 2008 at 07:25:13 Pacific
Subject: file_get_content command
Reply: (edit)

Hey Micheal thank you for the response. I tried what you said but I keep getting a blank output in my php page. This is what I did:
in a test HTML page I put (replaced [] with <>):

Unwanted text
$start_text = "[!--START--]";
Wanted text....
$end_text = "[!--END--]";
Unwanted text

and in my test php page I have:

<?php

$source=file_get_contents(path/page.html);
$start_pos = strpos($start_text, $search) + strlen($search);
$end_pos = strpos($end_text, $search) - $start_pos;
$found_text = substr($source, $start_pos, $end_pos);

echo $found_text;

?>

Do I need php markups before my start_text and after end_text lines in my HTML document? Also, I don't really get what string is being stored in the $search variable...


Report Offensive Follow Up For Removal

Response Number 3
Name: Michael J (by mjdamato)
Date: January 4, 2008 at 09:54:17 Pacific
Subject: file_get_content command
Reply: (edit)

You state that

Unwanted text
$start_text = "[!--START--]";
Wanted text....
$end_text = "[!--END--]";
Unwanted text

I'm assuming that was just a typo and you are only putting in the comment code and not the whole PHP code.

The $search variable is supposed to be the contents of the HTML file - i.e. what you are calling $source. Anyway I had a typo in my code and transposed the two variables in the strpos() functions.

I have corrected and tested the following code:
HTML file (source.htm)

[html]
[head][/head]

[body]

Unwanted Text
[!--START--]
Wanted Text
[!--END--]
Unwanted Text

[/body]
[/html]

PHP Fiel

<?php


$start_text = "[!--START--]";
$end_text = "[!--END--]";


$source = file_get_contents('source.htm');


$start_pos = strpos($source, $start_text) + strlen($start_text);
$end_pos = strpos($source, $end_text) - $start_pos;


$found_text = substr($source, $start_pos, $end_pos);


echo $found_text;
//Output: "Wanted text"


?>


Michael J


Report Offensive Follow Up For Removal

Response Number 4
Name: salehramazani
Date: January 4, 2008 at 14:35:59 Pacific
Subject: file_get_content command
Reply: (edit)

Thanks Micheal, that worked! I had it kind of mixed up, I see exactly how it works now!


Report Offensive Follow Up For Removal

Response Number 5
Name: niltonheck
Date: February 27, 2008 at 07:48:07 Pacific
Subject: file_get_content command
Reply: (edit)

A question.
This source work as well if im using a local file, or better if the pages are used in the same server.
But, what can i do when the "source.htm" is outside of the server in another website?


Report Offensive Follow Up For Removal


Response Number 6
Name: Michael J (by mjdamato)
Date: February 27, 2008 at 07:53:37 Pacific
Subject: file_get_content command
Reply: (edit)

If your PHP installation supports it you can specify a URL within the file_get_contents() command.

Michael J


Report Offensive Follow Up For Removal

Response Number 7
Name: niltonheck
Date: February 27, 2008 at 08:04:06 Pacific
Subject: file_get_content command
Reply: (edit)

What i need is:
Get the HTML from another page, and "cut" a part of it.
To show only what i want.
I do it as you say later but its working only when the page is in the same machine, and not when the page that i need get the html is outside of the machine.
i hate it. x)
Im working on it for a long time. =/


Report Offensive Follow Up For Removal

Response Number 8
Name: Michael J (by mjdamato)
Date: February 27, 2008 at 09:51:10 Pacific
Subject: file_get_content command
Reply: (edit)

Try running this file. Do you get "Yahoo!" echo'd to the page?

<?php

$start_text = "<title>";
$end_text = "</title>";

$source = file_get_contents('http://www.yahoo.com');

$start_pos = strpos($source, $start_text) + strlen($start_text);
$end_pos = strpos($source, $end_text) - $start_pos;

$found_text = substr($source, $start_pos, $end_pos);

echo $found_text;
//Output: "Yahoo!"

?>

Michael J


Report Offensive Follow Up For Removal






Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: file_get_content command

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software