Get content from distant URL and test - php

I have to get data from a .html file, into a distant server. With file_gets_content I can retrieve the informations but when I want to test it I have some problems.
For example I can have 0 or 1 into my .html page. In my .php I want to do something if the file_gets_content return 0 or 1 but for now I didn't find how I can do it
Here is my .html :
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>test</title>
</head>
<body>
0
</body>
</html>
My PHP code :
$home = file_get_contents('http://192.168.1.XXX/wordpress/read.html');

You can use this php library https://github.com/sunra/php-simple-html-dom-parser
$dom =HtmlDomParser::file_get_html('http://192.168.1.XXX/wordpress/read.html');
$bodyText=$dom->find("body",0)->innertext;
alternative solution is
$home = file_get_contents('http://192.168.1.XXX/wordpress/read.html');
$dom = new DOMDocument();
$dom->loadHTML($home);
if(($body=$dom->getElementsByTagName("body"))->length>0){
$text=$body[0]->nodeValue
}

Related

Page Title showing ASCII character

am using below to show browser page title on a joomla website. The only issue occurs when there are apostrophe in the title.
$browserpagetitle= 'My site - '.$this->item->title;
$document = JFactory::getDocument();
$document->setTitle($browserpagetitle);
If the item title is Apple's. it will show : My site - Apple's
I have tried :
$browserpagetitle= 'My site - '.$this->item->title;
$document = JFactory::getDocument();
echo html_entity_decode($document->setTitle($browserpagetitle), ENT_QUOTES);
as suggested here but no luck
Add
<meta charset="utf-8" />
immediately after your opening
<html>
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<title>*page title here*</title>
</head>
Please try below code
$browserpagetitle= 'My site - '.$this->item->title;
$document = JFactory::getDocument();
$document->setTitle(htmlspecialchars_decode($browserpagetitle,ENT_QUOTES));

How do i make Xpath 1.0 query case insensitive

In PHP, I'm currently making a xpath query but I need to make it case insensitive.
I'm using is XPath 1.0 which from my query means I've got to use some thing called a translate function but I'm unsure of how to do this.
Here is my query test PHP file :
$html = <<<'HTML'
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<meta NAME="Description" content="Test Case">
<META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW">
<Link Rel="Canonical" href="http://www.testsite.com/" />
<Title>My Title</Title>
</head>
<Body>
Test Case
</Body>
</html>
HTML;
$domDoc = new DOMDocument();
$domDoc->loadHTML('<?xml encoding="utf-8" ?>' . $html);
// Canonical link
$xpath = new DOMXPath($domDoc);
$canonicalTags = $xpath->query('//link[#rel=\'canonical\']'); // Return nothing
//some use translate(WhatVariable?, 'ABCDEFGHIJKLMNOPQRSTUVWXYZÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞŸŽŠŒ', 'abcdefghijklmnopqrstuvwxyzàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþÿžšœ')
var_dump($canonicalTags);
Any help would be greatly appreciated. Thanks.
Basically, translate is used to convert dynamic value that you need to compare to be all lower-case (or all upper-case). In this case, you want to apply translate() to rel attribute value, and compare the result to lower-case literal "canonical" (formatted for readability) :
//link[
translate(#rel, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz') = 'canonical'
]

PHP - Extract data from string with regex

I need help to do this operation. I Have a string like this:
<!doctype html> <html> <head> <meta charset="utf-8"> <title>Formatting the report</title><meta http-equiv="refresh" content="5;url=/file/xslt/download/?fileName=somename.pdf"> </head>
I need to extract the fileName parameter. How to do this?
I thing that is possible with regex, but I do not know well this.
Thanks!
Try this..
This will capture the filename
The Pattern is given below
/fileName=(.+?)\"/
<?php
$subject = "<!doctype html> <html> <head> <meta charset="utf-8"> <title>Formatting the report</title><meta http-equiv="refresh" content="5;url=/file/xslt/download/?fileName=somename.pdf"> </head>";
$pattern = '/fileName=(.+)"/';
preg_match($pattern, $subject, $matches, PREG_OFFSET_CAPTURE, 2);
print_r($matches);
?>
$1->Contains the file name
demo
Try something along the lines of:
$str = '<!doctype html> <html> <head> <meta charset="utf-8"> <title>Formatting the report</title><meta http-equiv="refresh" content="5;url=/file/xslt/download/?fileName=somename.pdf"> </head>';
preg_match('#fileName=(.*)"#', $str, $matches);
print_r($matches);
php simple html dom is clean and good way for trace html and find html elements by selector's like Jquery selectors.

string's result is different after load in domdocument

I want to have same result after load in domdocument. how to do it?
echo "Café";
$s = <<<HTML
<html>
<head>
</head>
<body>
Café
</body>
</html>
HTML;
$d = new domdocument;
$d->loadHTML($s);
echo $d->textContent;
first echo's result is = Café
second echo's result is =Café
You need to mark your HTML as UTF-8 encoded
$s = <<<HTML
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
</head>
<body>
Café
</body>
</html>
HTML;
$d = new domdocument;
$d->loadHTML($s);
echo $d->textContent;
your problem is Encoding,
for the First Echo, you echo the text with your default encoding,
but for the text randered through the DOMDocument,
the e+apostroph is split into two chars,
i dont know how to enforce the right encoding to DOMDoc...
but i am sure this is your problem
hope i helped,
best of luck.
With First echo before HTML you send HEADERS with your server default encoding. This ignores any next set encodings..
You must first echo
<Html tag and encodings etc..
and than echo any other values..

Template lite tags

I really searched but found nothing.
I'm new at template lite. I add my project template_lite library and I have two files.
test.php is:
require("../src/class.template.php");
$tpl = new Template_Lite;
$tpl->assign("foo","bar");
and test.html is
<html>
<head>
<title>Document Title</title>
</head>
<body>
{$foo}
</body>
</html>
what the wrong is output:"{$foo}"
According to the documentation you need to set two variables:
There are two variables that you need to set after loading Template
Lite: $template_dir and $compile_dir.
require('/path/to/class.template.php');
$tpl = new Template_Lite;
$tpl->compile_dir = "compiled/";
$tpl->template_dir = "templates/";

Categories