在php中回显带有标签的图像url

发布于 2024-09-10 06:59:08 字数 376 浏览 2 评论 0原文

我之前问过一个关于如何从 html 页面回显图像 url 的问题。我可以成功地做到这一点,但如何进一步缩小范围,以便只显示以某个短语开头的图像网址,此外,如何在它们周围添加图像标签,以便图像显示为图像而不仅仅是文本?

例如,我只想列出以 http://photos.website.com 开头的图像。

编辑:我忘了提及这是用于迭代图像的代码:

foreach($images as $image) {
    echo $image->getAttribute('src') . '<br />';
}

I have previously asked a question on how to echo the url of images from an html page. I can do this successfully but how can I narrow this down further so only images urls beginning with a certain phrase are shown, furthermore how can I add an image tag around them so the images are displayed as images and not just text?

e.g I only want to list images beginning with http://photos.website.com.

edit: I forgot to mention this is the code used to iterate through the images:

foreach($images as $image) {
    echo $image->getAttribute('src') . '<br />';
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

弄潮 2024-09-17 06:59:08

您必须添加一个条件来测试 $image->getAttribute('src') 的内容。

要测试一个字符串是否被另一个字符串存在,可以使用 strpos 函数,它返回干草堆的位置 - 在这里,您希望该位置为0 (即字符串的第一个字符)

foreach($images as $image) {
    $url = $image->getAttribute('src');
    if (strpos($url, 'http://photos.website.com') === 0) {
        echo $url . '<br />';
    }
}

You will have to add a condition that tests the content of $image->getAttribute('src').

To test if a string beings by another, a possibility is to use the strpos function, which returns the position of the needle in the haystack -- here, you want that position to be 0 (i.e. the first character of the string).

foreach($images as $image) {
    $url = $image->getAttribute('src');
    if (strpos($url, 'http://photos.website.com') === 0) {
        echo $url . '<br />';
    }
}
榕城若虚 2024-09-17 06:59:08

简单:

foreach ($images as $image) {
    $src = $image->getAttribute('src');

    if (stripos($src, 'http://photos.website.com') === 0)
    {
        echo $src . '<br />';
    }
}

添加标签:

foreach ($images as $image) {
    $src = $image->getAttribute('src');

    if (stripos($src, 'http://photos.website.com') === 0)
    {
        echo sprintf('<img src="%s" alt="" />', $src) . "\n";
    }
}

Simple:

foreach ($images as $image) {
    $src = $image->getAttribute('src');

    if (stripos($src, 'http://photos.website.com') === 0)
    {
        echo $src . '<br />';
    }
}

And to add tags:

foreach ($images as $image) {
    $src = $image->getAttribute('src');

    if (stripos($src, 'http://photos.website.com') === 0)
    {
        echo sprintf('<img src="%s" alt="" />', $src) . "\n";
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文