【问题标题】:Extract string using htmldom php使用 htmldom php 提取字符串
【发布时间】:2018-10-07 00:11:41
【问题描述】:

如何在 php 中使用 html dom 提取特定单词后的特定字符串。我有

<div class="abc">    
<script type="text/javascript">
var flashvars = {    word : path  }  </script>

现在我想在单词之后提取路径

【问题讨论】:

  • console.log(flashvars.word)
  • 也许这可以帮助你:stackoverflow.com/questions/5126967/…
  • 我正在尝试提取我稍后在代码中需要的路径。这些 sn-ps 从特定标签中提取。但是,我需要一些可以在 html 源代码中的确切“单词”之后提取路径的东西。
  • this 你在找什么?

标签: php html extract


【解决方案1】:

感谢您的回复。 我得到了我正在寻找的解决方案。

这是代码,以防有人需要。

Explanation : '$results' is the curl response.

Enter div class name (which you want to fetch) inside "$xpath-&gt;query() function"

You will get source code for entire class inside "$tag-&gt;textContent"

    $dom = new DOMDocument();
    $dom->loadHTML($results);
    $xpath = new DOMXPath($dom);
    $tags = $xpath->query('//div[@class="e"]');
    foreach ($tags as $tag)
   {
    echo "<br>----------<br>";
    var_dump($tag->textContent);
    echo "<br>----------<br>";
    }

现在您在“$tag->textContent”中拥有了所需类的 html 源代码。

现在您可以使用以下函数从“开始”和“结束”点之间的字符串中获取任何内容。

function get_string_between($string, $start, $end){
   $string = ' ' . $string;
   $ini = strpos($string, $start);
   if ($ini == 0) return '';
   $ini += strlen($start);
   $len = strpos($string, $end, $ini) - $ini;
   return substr($string, $ini, $len);
}

就我而言,我是这样使用它的:

$price = get_string_between($tag->textContent,'swf', '+'); echo $price;

这里“swf”是路径的起点,“+”是终点。 希望它可以节省其他人的时间:)

【讨论】:

    猜你喜欢
    • 2017-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-24
    • 1970-01-01
    相关资源
    最近更新 更多