how to dom parser html table which containing “<” symbol in php
I want to make curl in php to get the data in html table on this website: https://bri.co.id/web/guest/deposit-interest-rate.
Here's the code I was trying:
<?php
error_reporting(E_ALL);
$url = "https://bri.co.id/web/guest/deposit-interest-rate";
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$html = curl_exec($ch);
curl_close($ch);
$dom = new DOMDocument();
$dom->preserveWhiteSpace = false;
$dom->validateOnParse = true;
$dom->formatOutput = true;
libxml_use_internal_errors(true);
@$dom->loadHTML($html);
$table = $dom->getElementById('_Deposit_Rate_Display_Portlet_idrRate');
$row = $table->getElementsByTagName('tr')[1];
//var_dump($row);
$cols = $row->getElementsByTagName('td');
echo 'NOMINAL -> ' . $cols->item(0)->nodeValue. '<br />';
echo 'JANGKA WAKTU -> ' . $cols->item(1)->nodeValue.'<br />';
echo 'SUKU BUNGA COUNTER -> ' . $cols->item(2)->nodeValue;
?>
the result is:
NOMINAL ->
JANGKA WAKTU -> 1
SUKU BUNGA COUNTER -> 4.75%
I want the result is:
NOMINAL -> <100 Juta
JANGKA WAKTU -> 1
SUKU BUNGA COUNTER -> 4.75%
the problem is the data which contain "<" symbol not showing its show empty string.
How to dom parser "<" symbol so can show the output "<100 Juta" ?
php curl dom html-table html-parsing
add a comment |
I want to make curl in php to get the data in html table on this website: https://bri.co.id/web/guest/deposit-interest-rate.
Here's the code I was trying:
<?php
error_reporting(E_ALL);
$url = "https://bri.co.id/web/guest/deposit-interest-rate";
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$html = curl_exec($ch);
curl_close($ch);
$dom = new DOMDocument();
$dom->preserveWhiteSpace = false;
$dom->validateOnParse = true;
$dom->formatOutput = true;
libxml_use_internal_errors(true);
@$dom->loadHTML($html);
$table = $dom->getElementById('_Deposit_Rate_Display_Portlet_idrRate');
$row = $table->getElementsByTagName('tr')[1];
//var_dump($row);
$cols = $row->getElementsByTagName('td');
echo 'NOMINAL -> ' . $cols->item(0)->nodeValue. '<br />';
echo 'JANGKA WAKTU -> ' . $cols->item(1)->nodeValue.'<br />';
echo 'SUKU BUNGA COUNTER -> ' . $cols->item(2)->nodeValue;
?>
the result is:
NOMINAL ->
JANGKA WAKTU -> 1
SUKU BUNGA COUNTER -> 4.75%
I want the result is:
NOMINAL -> <100 Juta
JANGKA WAKTU -> 1
SUKU BUNGA COUNTER -> 4.75%
the problem is the data which contain "<" symbol not showing its show empty string.
How to dom parser "<" symbol so can show the output "<100 Juta" ?
php curl dom html-table html-parsing
What does the HTML look like?
– miken32
Nov 25 '18 at 4:15
The issue is the <, which should be encoded as an HTMLEntity (<). Because it is not encoded, the node isn't parsing correctly.
– user2182349
Nov 25 '18 at 5:30
add a comment |
I want to make curl in php to get the data in html table on this website: https://bri.co.id/web/guest/deposit-interest-rate.
Here's the code I was trying:
<?php
error_reporting(E_ALL);
$url = "https://bri.co.id/web/guest/deposit-interest-rate";
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$html = curl_exec($ch);
curl_close($ch);
$dom = new DOMDocument();
$dom->preserveWhiteSpace = false;
$dom->validateOnParse = true;
$dom->formatOutput = true;
libxml_use_internal_errors(true);
@$dom->loadHTML($html);
$table = $dom->getElementById('_Deposit_Rate_Display_Portlet_idrRate');
$row = $table->getElementsByTagName('tr')[1];
//var_dump($row);
$cols = $row->getElementsByTagName('td');
echo 'NOMINAL -> ' . $cols->item(0)->nodeValue. '<br />';
echo 'JANGKA WAKTU -> ' . $cols->item(1)->nodeValue.'<br />';
echo 'SUKU BUNGA COUNTER -> ' . $cols->item(2)->nodeValue;
?>
the result is:
NOMINAL ->
JANGKA WAKTU -> 1
SUKU BUNGA COUNTER -> 4.75%
I want the result is:
NOMINAL -> <100 Juta
JANGKA WAKTU -> 1
SUKU BUNGA COUNTER -> 4.75%
the problem is the data which contain "<" symbol not showing its show empty string.
How to dom parser "<" symbol so can show the output "<100 Juta" ?
php curl dom html-table html-parsing
I want to make curl in php to get the data in html table on this website: https://bri.co.id/web/guest/deposit-interest-rate.
Here's the code I was trying:
<?php
error_reporting(E_ALL);
$url = "https://bri.co.id/web/guest/deposit-interest-rate";
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$html = curl_exec($ch);
curl_close($ch);
$dom = new DOMDocument();
$dom->preserveWhiteSpace = false;
$dom->validateOnParse = true;
$dom->formatOutput = true;
libxml_use_internal_errors(true);
@$dom->loadHTML($html);
$table = $dom->getElementById('_Deposit_Rate_Display_Portlet_idrRate');
$row = $table->getElementsByTagName('tr')[1];
//var_dump($row);
$cols = $row->getElementsByTagName('td');
echo 'NOMINAL -> ' . $cols->item(0)->nodeValue. '<br />';
echo 'JANGKA WAKTU -> ' . $cols->item(1)->nodeValue.'<br />';
echo 'SUKU BUNGA COUNTER -> ' . $cols->item(2)->nodeValue;
?>
the result is:
NOMINAL ->
JANGKA WAKTU -> 1
SUKU BUNGA COUNTER -> 4.75%
I want the result is:
NOMINAL -> <100 Juta
JANGKA WAKTU -> 1
SUKU BUNGA COUNTER -> 4.75%
the problem is the data which contain "<" symbol not showing its show empty string.
How to dom parser "<" symbol so can show the output "<100 Juta" ?
php curl dom html-table html-parsing
php curl dom html-table html-parsing
asked Nov 25 '18 at 3:40
codercoder
104
104
What does the HTML look like?
– miken32
Nov 25 '18 at 4:15
The issue is the <, which should be encoded as an HTMLEntity (<). Because it is not encoded, the node isn't parsing correctly.
– user2182349
Nov 25 '18 at 5:30
add a comment |
What does the HTML look like?
– miken32
Nov 25 '18 at 4:15
The issue is the <, which should be encoded as an HTMLEntity (<). Because it is not encoded, the node isn't parsing correctly.
– user2182349
Nov 25 '18 at 5:30
What does the HTML look like?
– miken32
Nov 25 '18 at 4:15
What does the HTML look like?
– miken32
Nov 25 '18 at 4:15
The issue is the <, which should be encoded as an HTMLEntity (<). Because it is not encoded, the node isn't parsing correctly.
– user2182349
Nov 25 '18 at 5:30
The issue is the <, which should be encoded as an HTMLEntity (<). Because it is not encoded, the node isn't parsing correctly.
– user2182349
Nov 25 '18 at 5:30
add a comment |
1 Answer
1
active
oldest
votes
This will get the values - but it's not an ideal approach because if the HTML on the page changes, it will fail.
<?php
// Get the HTML
$url = "https://bri.co.id/web/guest/deposit-interest-rate";
$html = file_get_contents($url);
// Find the Rupiah header and discard everything before that
$rupiah = strpos($html,'<h2> Rupiah </h2>');
$chop = substr($html,$rupiah);
// Find he start and end of the Rupiah table
$tableStart = strpos($chop,'<table');
$tableEnd = strpos($chop,'</table>');
// Get the Rupiah table
$table = substr($chop,$tableStart,$tableEnd-$tableStart);
// Get the body of the Rupiah table
$tbodyStart = strpos($table,'<tbody>');
$tbody = substr($table,$tbodyStart+7);
// Get the rows
$rows = explode('<tr>',$tbody);
// Loop through all the rows and when you find the first blank one, get the cells
foreach ($rows as $r) {
if (trim($r) !== '') {
$cells = preg_split('#<td[^>]+>#',$r);
break;
}
}
// Loop through all the cells and echo out their contents (without any HTML tags)
foreach ($cells as $c) {
if (trim($c) !== '') {
echo strip_tags($c).PHP_EOL;
}
}
then how to make it an ideal ?
– coder
Nov 25 '18 at 9:08
Find a different way to get the rates. Is there an API/RSS feed you could read? If this is a school project, the code is good enough. It does work. For every piece of code you write, you have to decide how much time you want to spend on it after you get it working. Als, things change, so today’s perfect code may not work tomorrow.
– user2182349
Nov 25 '18 at 12:11
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53464455%2fhow-to-dom-parser-html-table-which-containing-symbol-in-php%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
This will get the values - but it's not an ideal approach because if the HTML on the page changes, it will fail.
<?php
// Get the HTML
$url = "https://bri.co.id/web/guest/deposit-interest-rate";
$html = file_get_contents($url);
// Find the Rupiah header and discard everything before that
$rupiah = strpos($html,'<h2> Rupiah </h2>');
$chop = substr($html,$rupiah);
// Find he start and end of the Rupiah table
$tableStart = strpos($chop,'<table');
$tableEnd = strpos($chop,'</table>');
// Get the Rupiah table
$table = substr($chop,$tableStart,$tableEnd-$tableStart);
// Get the body of the Rupiah table
$tbodyStart = strpos($table,'<tbody>');
$tbody = substr($table,$tbodyStart+7);
// Get the rows
$rows = explode('<tr>',$tbody);
// Loop through all the rows and when you find the first blank one, get the cells
foreach ($rows as $r) {
if (trim($r) !== '') {
$cells = preg_split('#<td[^>]+>#',$r);
break;
}
}
// Loop through all the cells and echo out their contents (without any HTML tags)
foreach ($cells as $c) {
if (trim($c) !== '') {
echo strip_tags($c).PHP_EOL;
}
}
then how to make it an ideal ?
– coder
Nov 25 '18 at 9:08
Find a different way to get the rates. Is there an API/RSS feed you could read? If this is a school project, the code is good enough. It does work. For every piece of code you write, you have to decide how much time you want to spend on it after you get it working. Als, things change, so today’s perfect code may not work tomorrow.
– user2182349
Nov 25 '18 at 12:11
add a comment |
This will get the values - but it's not an ideal approach because if the HTML on the page changes, it will fail.
<?php
// Get the HTML
$url = "https://bri.co.id/web/guest/deposit-interest-rate";
$html = file_get_contents($url);
// Find the Rupiah header and discard everything before that
$rupiah = strpos($html,'<h2> Rupiah </h2>');
$chop = substr($html,$rupiah);
// Find he start and end of the Rupiah table
$tableStart = strpos($chop,'<table');
$tableEnd = strpos($chop,'</table>');
// Get the Rupiah table
$table = substr($chop,$tableStart,$tableEnd-$tableStart);
// Get the body of the Rupiah table
$tbodyStart = strpos($table,'<tbody>');
$tbody = substr($table,$tbodyStart+7);
// Get the rows
$rows = explode('<tr>',$tbody);
// Loop through all the rows and when you find the first blank one, get the cells
foreach ($rows as $r) {
if (trim($r) !== '') {
$cells = preg_split('#<td[^>]+>#',$r);
break;
}
}
// Loop through all the cells and echo out their contents (without any HTML tags)
foreach ($cells as $c) {
if (trim($c) !== '') {
echo strip_tags($c).PHP_EOL;
}
}
then how to make it an ideal ?
– coder
Nov 25 '18 at 9:08
Find a different way to get the rates. Is there an API/RSS feed you could read? If this is a school project, the code is good enough. It does work. For every piece of code you write, you have to decide how much time you want to spend on it after you get it working. Als, things change, so today’s perfect code may not work tomorrow.
– user2182349
Nov 25 '18 at 12:11
add a comment |
This will get the values - but it's not an ideal approach because if the HTML on the page changes, it will fail.
<?php
// Get the HTML
$url = "https://bri.co.id/web/guest/deposit-interest-rate";
$html = file_get_contents($url);
// Find the Rupiah header and discard everything before that
$rupiah = strpos($html,'<h2> Rupiah </h2>');
$chop = substr($html,$rupiah);
// Find he start and end of the Rupiah table
$tableStart = strpos($chop,'<table');
$tableEnd = strpos($chop,'</table>');
// Get the Rupiah table
$table = substr($chop,$tableStart,$tableEnd-$tableStart);
// Get the body of the Rupiah table
$tbodyStart = strpos($table,'<tbody>');
$tbody = substr($table,$tbodyStart+7);
// Get the rows
$rows = explode('<tr>',$tbody);
// Loop through all the rows and when you find the first blank one, get the cells
foreach ($rows as $r) {
if (trim($r) !== '') {
$cells = preg_split('#<td[^>]+>#',$r);
break;
}
}
// Loop through all the cells and echo out their contents (without any HTML tags)
foreach ($cells as $c) {
if (trim($c) !== '') {
echo strip_tags($c).PHP_EOL;
}
}
This will get the values - but it's not an ideal approach because if the HTML on the page changes, it will fail.
<?php
// Get the HTML
$url = "https://bri.co.id/web/guest/deposit-interest-rate";
$html = file_get_contents($url);
// Find the Rupiah header and discard everything before that
$rupiah = strpos($html,'<h2> Rupiah </h2>');
$chop = substr($html,$rupiah);
// Find he start and end of the Rupiah table
$tableStart = strpos($chop,'<table');
$tableEnd = strpos($chop,'</table>');
// Get the Rupiah table
$table = substr($chop,$tableStart,$tableEnd-$tableStart);
// Get the body of the Rupiah table
$tbodyStart = strpos($table,'<tbody>');
$tbody = substr($table,$tbodyStart+7);
// Get the rows
$rows = explode('<tr>',$tbody);
// Loop through all the rows and when you find the first blank one, get the cells
foreach ($rows as $r) {
if (trim($r) !== '') {
$cells = preg_split('#<td[^>]+>#',$r);
break;
}
}
// Loop through all the cells and echo out their contents (without any HTML tags)
foreach ($cells as $c) {
if (trim($c) !== '') {
echo strip_tags($c).PHP_EOL;
}
}
answered Nov 25 '18 at 5:31
user2182349user2182349
7,22321633
7,22321633
then how to make it an ideal ?
– coder
Nov 25 '18 at 9:08
Find a different way to get the rates. Is there an API/RSS feed you could read? If this is a school project, the code is good enough. It does work. For every piece of code you write, you have to decide how much time you want to spend on it after you get it working. Als, things change, so today’s perfect code may not work tomorrow.
– user2182349
Nov 25 '18 at 12:11
add a comment |
then how to make it an ideal ?
– coder
Nov 25 '18 at 9:08
Find a different way to get the rates. Is there an API/RSS feed you could read? If this is a school project, the code is good enough. It does work. For every piece of code you write, you have to decide how much time you want to spend on it after you get it working. Als, things change, so today’s perfect code may not work tomorrow.
– user2182349
Nov 25 '18 at 12:11
then how to make it an ideal ?
– coder
Nov 25 '18 at 9:08
then how to make it an ideal ?
– coder
Nov 25 '18 at 9:08
Find a different way to get the rates. Is there an API/RSS feed you could read? If this is a school project, the code is good enough. It does work. For every piece of code you write, you have to decide how much time you want to spend on it after you get it working. Als, things change, so today’s perfect code may not work tomorrow.
– user2182349
Nov 25 '18 at 12:11
Find a different way to get the rates. Is there an API/RSS feed you could read? If this is a school project, the code is good enough. It does work. For every piece of code you write, you have to decide how much time you want to spend on it after you get it working. Als, things change, so today’s perfect code may not work tomorrow.
– user2182349
Nov 25 '18 at 12:11
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53464455%2fhow-to-dom-parser-html-table-which-containing-symbol-in-php%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What does the HTML look like?
– miken32
Nov 25 '18 at 4:15
The issue is the <, which should be encoded as an HTMLEntity (<). Because it is not encoded, the node isn't parsing correctly.
– user2182349
Nov 25 '18 at 5:30