通过JS提取定界文本之间的HTML并显示

huangapple go评论54阅读模式
英文:

Fetching html between delimiter text and display via js

问题

我尝试使用JS在两个定界符之间(这里是"tools"和"hobbies")获取HTML,但没有显示任何内容:

fetch('https://ry3yr.github.io/OSTR/Diarykeepers_Homepage/Cool_Stuff.html')
  .then(response => response.text())
  .then(data => {
    const start = data.indexOf('Tools');
    const end = data.indexOf('Hobbies');
    const result = data.substring(start, end);
    document.getElementById('output').innerHTML = result;
  })

我在PHP中成功实现了这个功能:

<?php
$url = 'https://ry3yr.github.io/OSTR/Diarykeepers_Homepage/Cool_Stuff.html';
$html = file_get_contents($url);
$start = strpos($html, '<!------Tools--------->');
$end = strpos($html, '<!-----Hobbies---->', $start);
$length = $end - $start;
$result = substr($html, $start, $length);
echo $result;
?>

所以:
如何说服JS执行其任务?

英文:

I attempted fetching html between two delimiters (here "tools" and "hobbies") via JS, but there is nothing displayed:

fetch(&#39;https://ry3yr.github.io/OSTR/Diarykeepers_Homepage/Cool_Stuff.html&#39;)
  .then(response =&gt; response.text())
  .then(data =&gt; {
    const start = data.indexOf(&#39;Tools&#39;);
    const end = data.indexOf(&#39;Hobbies&#39;);
    const result = data.substring(start, end); document.getElementById(&#39;output&#39;).innerHTML = result;
  })

I *do have it working in php:

&lt;?php
$url = &#39;https://ry3yr.github.io/OSTR/Diarykeepers_Homepage/Cool_Stuff.html&#39;;
$html = file_get_contents($url);
$start = strpos($html, &#39;&lt;!------Tools----------&gt;&#39;);
$end = strpos($html, &#39;&lt;!-----Hobbies----&gt;&#39;, $start);
$length = $end - $start;
$result = substr($html, $start, $length);
echo $result;
?&gt;

So:
How can I convince JS to do its job ?

答案1

得分: 2

我有一种感觉,JS 版本的 indexOf 参数与您在 PHP 上使用的参数不同。

这应该可以解决问题:

fetch('https://ry3yr.github.io/OSTR/Diarykeepers_Homepage/Cool_Stuff.html')
  .then(response => response.text())
  .then(html => {
    const start = html.indexOf('<!--Tools-->');
    const end = html.indexOf('<!--Hobbies-->', start);
    const result = html.substring(start, end);
    console.log(result);
  })
  .catch(error => {
    console.error('Error:', error);
  });
英文:

I have the feeling the indexOf parameters for the JS version are different than the ones you using on PHP.

This should do the trick:

fetch(&#39;https://ry3yr.github.io/OSTR/Diarykeepers_Homepage/Cool_Stuff.html&#39;)
  .then(response =&gt; response.text())
  .then(html =&gt; {
    const start = html.indexOf(&#39;&lt;!------Tools----------&gt;&#39;);
    const end = html.indexOf(&#39;&lt;!-----Hobbies----&gt;&#39;, start);
    const result = html.substring(start, end);
    console.log(result);
  })
  .catch(error =&gt; {
    console.error(&#39;Error:&#39;, error);
  });

huangapple
  • 本文由 发表于 2023年5月28日 16:55:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76350695.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定