【问题标题】:(php get content ) how can i use foreach loop to rid retrieving one string(php获取内容)我如何使用foreach循环来摆脱检索一个字符串
【发布时间】:2018-08-25 20:11:25
【问题描述】:

我正在尝试从 url 获取某个表格的内容,但它总是得到一个字符串。

我想知道是否可以使用 foreach 循环来获取表中的所有字符串,例如:

<?php
$url = 'https://www.w3.org/WAI/UA/2002/06/thead-test';
$content = file_get_contents($url);
$first_step = explode( '<td>' , $content );
$second_step = explode('</td>' , $first_step[1] );
$output = $second_step[0];
echo $output;
?>

经过一番研究,我找到了一些链接,但对我没有太大帮助。

任何帮助表示赞赏:)

【问题讨论】:

    标签: php url foreach file-get-contents


    【解决方案1】:

    不完全清楚你在这里做什么......你想获取表格单元格之间所有字符串的内容(&lt;td&gt;xxx&lt;/td&gt;)?

    $url = 'https://www.w3.org/WAI/UA/2002/06/thead-test';
    $content = file_get_contents($url);
    preg_match_all('#<td>(.*?)</td>#m', $content, $matches);
    $output = $matches[1];
    var_dump($output);
    

    【讨论】:

    • 是的。但我做不到。甚至您的代码也不成功:/
    • 假设获取此 url 的内容。 zabihullah.com/watch/yt/txt.php我想得到

      x

      之间的所有字符串,从 1 到 5
    • 哎呀;对不起。 preg_match_all 总是让我惊讶它是如何构建它的匹配数组的。上面已编辑并已修复。
    • 谢谢。你能输出纯txt吗?
    • echo implode(' ', $matches[1]);
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-06
    • 1970-01-01
    • 2019-01-15
    • 1970-01-01
    • 2022-12-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多