Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
For reasons I won't get into, I have a simple HTML page that is updated frequently, but is deep inside my site. I want to post a substring (first 50 or so characters) on the main page of my website with a link to the page, this substring will update whenever I update the HTML page.
Is there a way to do this without incorporating a database and variables (using substring) on into the entire system? I'm looking for the easiest and least complex way of doing so.

Sure just use php to read the html file. You would just need to create rules on where the text you want to include begins. For example, does it start right after the bODY tag or later in the page. You will want to keep the HTML page consistent so that you don't have to update your php.
Michael J

Thanks Michael...
Keeping the HTML page consistent is no problem at all. It'd be a thing where I'd want the data between 2 places passed to another page, up to a certain number of characters.
I guess I'm curious as to what these "rules" you speak of would be. I don't know how to pass something to another page without it being stored in a mysql database (I've only used PHP to pass things to a page using SQL statements before)

You are not passing anything to the page. You would be using the php page to read the contents of the HTML page. Similar "concept" to pulling records from a database but, of course, you would not be using a query to do it.
this page ( http://us3.php.net/function.file-ge... ) explains the command file_get_contents to read the contents of a file. In this case you would se something like this:
$source = file_get_contents("path/file.html");
You now have the contents of the html file in a string variable. Then, by rules, I mean the logic to pull out the contnet you want. You can't just grap the first 50 characters unless you want it to start with "<html><head>...".
I would suggest that you put a comment tag right before the text section you want to grab. Then you can use the string functions to get the 50 characters like so (assuming text is preceeded by "";
$newText = substr($source,strpos($source, $search)+strlen($search), 50);Michael J

The forum consumed some of the code even though it was escaped. Here is is again with the <> replaced with [] in the search string (assuming text in the HTML file is preceeded by "[!--start text--]":
$search = "[!--start text--]";
$newText = substr($source,strpos($source, $search)+strlen($search), 50);Michael J

First off ... thanks for your help on this so far, I really really apprecaite it!!
I understand everything and see how it should work and how it is linked, but it doesn't appear to be working.
I have, in the HTML document .. the "start text" portion positioned where I want it ...
...then in the php test document, I have my $source and other variables set ... however it's coming up blank ....
you can view my source code of the 2 documents here:
http://www.fleetmack.com/2.0/script...(it's online as www.fleetmack.com/2.0/scripts/php_news_module.php )
also -- the html page is found at:
http://www.fleetmack.com/News.htm(yes, awful site I know, hence why after 6 years it's finally getting a good overhaul!)

The source code document you linked to is blank. Also, I looked at the code in the html page and dont see any apparent "start text" indication.
Michael J

OK, in your htm document, put this right before the text you want to capture (replace [] with <>):
[!--Start Text--]
Then in the php file put this (replace [] with <>):
if ($source = file_get_contents("pathtofile/News.htm")) {
$search = "[!--Start Text--]";
$newText = substr($source,strpos($source, $search)+strlen($search), 100);
echo $newText;
} else {
// Steps to take when couldn't read file
}
Michael J

Right on Michael, thanks again .. it seems to work fine in my PHP Developer 2007 program (in Debug view, but not Preview view, as I only have the free version so I can't see what it debugged), but on my website it appears blank. I'm wondering if that's due to the fact that my site is only on PHP 4.4.2?

I don't think so. What are you using for the "Start Text" identifier - I still don't see anything in the live page?
Try this. Create a test php page in the same directory as the "News.htm" and put in the following code:
<?
$source = file_get_contents("News.htm"))
echo $source;
?>When you navigate to the test.php page you should see exactly the same content as the News.htm page. If that works, then you can add one element of the code at a time to get to what you want.
Michael J

After backing everything up I just upgraded to PHP 5.0.5 and everything works wonderfully (turns out it did work before my reply asking you to clarity your code as well, fyi, just a php4 issue). Thanks so much for your help, Michael J - you are truly an asset to this community!!

to clarify, I'm an idiot ... i had a link to the wrong HTML page ... there was another page with the [!--Start Text--] segment (obviously, with <>). Thanks again for your help and sorry for the wrong link. (upgrading to 5.0.5 also fixed another php issue I was having)

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |