【问题标题】:space proplem in in the str_replacestr_replace 中的空间问题
【发布时间】:2020-03-09 15:02:15
【问题描述】:

我已经为图像 crwaler 制作了一个 wordpress 插件,但我在这段代码中有问题

所以当我打印 $image 我得到这个输出 https://localhost/wordpress

https://后面有个空格

我尝试了 str_replace 但没有消失

我想要结果https://localhost/wordpress

<?php
    function image_url_filter($url) {
        $url = str_replace('?ssl=1', '', $url);
        $url = str_replace('https://', '', $url);
        $url = str_replace('http://', '', $url);
        $url = str_replace('//', '', $url);
        $url = str_replace('http:', '', $url)
            return "https://{$url}";
        }

    function get_chapter_images() {
        include('simple_html_dom.php');
        $url = 'http://localhost/wordpress/manga/manga-name-ain/chapter-4/';
        $html = file_get_html($url);
        $images_url = array();
        foreach ($html->find('.page-break img') as $e) {
            $image_links = $e->src;
            array_push($images_url, image_url_filter($image_links));
        }
        //print_r($images_url);
        return $images_url;
    }

    $images_links = get_chapter_images();
    foreach ($images_links as $image) {
        print_r($image);
    }

【问题讨论】:

  • “我尝试了 str_replace 但没有消失” - 也许它不是一个实际的(或只是一个)空间......?首先检查你实际得到了什么——使用urlencode之类的东西来对值进行调试输出,百分比编码会很容易地告诉你你真正在处理什么。
  • (将来,请正确格式化您在此处发布的代码 - 包括正确的缩进。现在,很难一眼看出您拥有的代码中嵌套了什么显示。)
  • 有其他人在编写与您相同的代码,也许您可​​以互相帮助?见:stackoverflow.com/questions/60595966/…
  • 我得到这个 https%3A%2F%2F%09%09%09+%09%09%09localhost%2Fwordpress%2Fwp-content%2Fuploads%2FWP-manga%2Fdata%2Fmanga_5e62092804a6d%2Ff6954e41130c0015b5b595%5d 2F12.jpg

标签: php arrays wordpress string web-crawler


【解决方案1】:

%09+ 表示您的字符串中同时包含 tabspace,因此您需要使用 urldecode()str_replace() 来解决此问题:

<?php
    $url = 'https%3A%2F%2F%09%09%09+%09%09%09localhost%2Fwordpress%2Fwp-content%2Fuploads%2FWP-manga%2Fdata%2Fmanga_5e62092804a6d%2Ff6954e41130c0015b5b89a3021d55595%2F12.jpg';
    $url_decode = urldecode($url);
    $url_decode = str_replace(" ", "", $url_decode);
    $url_decode = str_replace("\t", "", $url_decode);
    echo $url_decode;

输出:

https://localhost/wordpress/wp-content/uploads/WP-manga/data/manga_5e62092804a6d/f6954e41130c0015b5b89a3021d55595/12.jpg

注意:替换tabnewline时不要忘记使用双倍配额

【讨论】:

    猜你喜欢
    • 2012-12-10
    • 1970-01-01
    • 1970-01-01
    • 2019-06-08
    • 2020-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多