Read a specific part of an HTML file - php

I want to load a specific part of an HTML file into a variable like this:
$my_file = 'file.html';
$handle = fopen($my_file, 'r') or die('Cannot open file: '.$my_file); //implicitly creates file
Now, my question is how do I tell PHP which part of the HTML doc I want it to read?

That depends on what you mean by "part".
The most usefull approaches:
If you know a unique string that starts and finishes that part, e.g. and , you can load the complete file into a string, search the occurence of the first string, search the occurnece of the second one, then take the substring inbetween.
If you have less information, you might need regex.
If your HTML is valid XML, load it into an xml object and use xpath or alike... you see, the answer much depends on what you have, and what you want to achieve.

Related

SimpleXML: Read an .xml file without having to give the full name of the file

I have the following code
$url ='pathfile/';
$file = glob($url . '.xml');
xml = simplexml_load_file($file) or die ("Can't connect to URL");
So what i am trying to achieve here, is to read with simpleXML an .xml file. I do not want to have an absolute path, because i change the xml every day and the name is always different.
So, how can i get the .xml file without having to give the files' name?
Best regards
glob() should allow you to use an asterisk as a wildcard, so for your purposes something like the following:
$url ='pathfile/';
$file = glob($url . '*.xml');
This is with the assumption that you replace the XML in the directory, rather than add it next to the older ones. As an additional note, glob() returns an array of matched files, so you'll want to loop through the $file variable above.

Fetching Limited Words In PHP Simple HTML DOM Parser

Have a look at this code
echo file_get_html('http://www.google.com)->plaintext;it will fetch full content from a website. So, my question is how to fetch limited words. Suppose it will fetch only 180 words.. any idea?
file_get_html creates a DOM object, which requires a full page load/parse so you can't necessarily grab by character count. You can, however, initiate a file handler and fread to a certain amount of bytes:
$fh = fopen('google.com');
$data = fread($fh,$length);
fclose($fh);
Or this:
$data = substr(file_get_contents('google.com'),$start,$end);

PHP - DOMDocument load XML with encoded name

Lets say that in my flash project I have script that create for me xml files dynamically (by PHP). XML file name is based on specific variable and escaped using escape(variable) in case that variable may (and mostly do) contains unsupported filename chars...
I need to know precise name of xml file later in my flash project, because I'm loading these XML files only if unescape(XMLfile) == variable . There's a lot of variables, so I can't just use String.replace() function to wipe out unsuported fileneme chars...
There's part of PHP file I'm using:
$XMLDom = new DomDocument('1.0', 'UTF-8');
$xmlId = trim($_POST['xmlId']);
if(file_exists($xmlId)){
$XMLDom ->load($xmlId);
}else{
$newXMLHandler = fopen($xmlId, 'w') or die("can't open file");
fclose($newXMLHandler);
$XMLDom ->load($xmlId);
.... rest of the code ....
$XMLDom ->save($xmlId);
}
The result of the code above is that in directory are 2 newly created XML files
One XML empty created by fopen($xmlId, 'w'), named: "fi%20le%2C%2E%40.xml"
and second one named: "fi le,.#.xml" where all my new XML data is stored...
Is there any way to load escaped named XML file by PHP?
Thanks in advance.
Arthur.
I don't feel quite confident I understand your problem, but if your question was to find the analogue function to escape() in PHP, then urlencode() looks like the best match, but you need to research what exactly is being escaped. Note, for example, that there are several different ways to percent-encode strings, especially the multibyte strings. Flash may use escapeMultibyte() or it can also use encodeURIComponent() both encode different subsets of characters, and differently - so beware!
Now, regarding file names, if your HTTP server is running on Unix system, than "fi le,.#.xml" is a valid file name, nothing to worry about - inconvenient some times, but it is a legitimate name.
touch 'fi le,.#.xml'
would create a file, no problems there. Basically, the restricted characters are the slashes and the null character ("\x00"), but it is common to restrict also the characters that may be interpreted as shell commands - this is really up to you.

Updating portion of text file with PHP

Is it possible to replace part of text or HTML file with PHP? I'm loading portion of file into text editor by using preg_match to extract text only between certain tags. Now when finish editing I want to update the same file with changes made, and replace the same part previously loaded.
since you're using preg_match, you can use the preg_replace after editing and store it to the file.
for example if you are loading a UI for that then you might be doing this on file1.php
$data = file_get_contents($filename);
//do regex here
$values = preg_match($pattern,$data);
//do necessary display here for the form I assume
then on file2.php that receives the request on the form, you do exacly the same thing
$data = file_get_contents($filename);
//compose the string to you will have to replace to the pattern
$data = preg_replace($pattern,$replace,$data);
//then write to the same file
file_put_contents($filename,$data);
these are just theoretical, kindly check with the php manual for correct syntax or parameters

php text file news updates

I am trying to make a news feed type thing in php.
I have a text file - news.txt and a php file index.php.
I have done the surrounding code and opening/closing the text file. Now I am stuck how to insert the new news item $newsnew to the top of the news.txt file and how to delete the old bottom news file in the news.txt file.
Is there any way to do this without deleting the whole file and writing it all again?
EDIT: Each news item is just a small string, say 500 characters, a single line.
Use a database.
If you really must use text files, use a different file for every news-item and name them sequentially like:
news001.txt
news002.txt
etc.
Then you can just add and delete files, read the directory and display what´s there.
Use the file() function to import the items in news.txt as an array, and use array_unshift() to add the new first item, and array_pop() to remove the last item. Join the array back into a single string and write it to news.txt:
$items = file('news.txt');
array_unshift($items, 'New item 1');
array_pop($items);
$newstext = implode(PHP_EOL, $items);
// write $newstext to the external file
If this is a XML file you could read it, parse it and delete the last child in the DOM. But if you have all your data in a DB it could be much easier to rewrite the file every time.
EDIT: after your edit: yes, you can do it like this:
write your new line to a new file
read the old file line by line and write it to the new one
skip the last line (detected by counting or EOF)
delete the old file and rename the new
No, there is not. But you might consider storing the messages in revers order. That way you only need to append to news.txt when new news arrive.
You are not going to be able to prepend to the beginning of the file without writing the whole thing out again. You could append to the end of it with the "a" mode flag to fopen(), but still to delete the oldest item you'll need to write out the entire file again.
Really, a database is solution here instead of a single text file.
There are many ways you can do it using the flat text file, but I'm not really sure it it's worth it. You can use some lightweight structured file or embedded database. For example SQLite, which would store it in normal file, no additional setup needed.

Categories