0

I have made a wordpress plugin for image crwaler but i have proplem in this code

so when i print $image i get this output https:// localhost/wordpress

there is a space after https://

i tryed str_replace but did not gone

i want the result 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);
    }
mitkosoft
  • 5,262
  • 1
  • 13
  • 31
  • _“i tryed str_replace but did not gone”_ - perhaps it wasn’t an actual (or just a single) space then …? Check what you actually got there first of all - use something like `urlencode` to make a debug output of the value, the percent encoding will easily show you what you are really dealing with. – CBroe Mar 09 '20 at 15:06
  • (And in the future, please format the code you post here properly - that include proper indentation. Right now, it is hard to see at a glance what is nested into what, in the code you have shown.) – CBroe Mar 09 '20 at 15:06
  • 2
    There's someone else working on the same code as you are, perhaps you can help each other? See: https://stackoverflow.com/questions/60595966/i-get-no-data-when-download-images#comment107214550_60595966 – KIKO Software Mar 09 '20 at 15:07
  • i get this https%3A%2F%2F%09%09%09+%09%09%09localhost%2Fwordpress%2Fwp-content%2Fuploads%2FWP-manga%2Fdata%2Fmanga_5e62092804a6d%2Ff6954e41130c0015b5b89a3021d55595%2F12.jpg – abbas ameer Mar 09 '20 at 15:11

1 Answers1

0

%09 and + means you have both tab and space in your string, so you need to use urldecode(), together with str_replace() to fix that:

<?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;

output:

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

Note: Don't forget to use double quotas when replacing tab or newline

mitkosoft
  • 5,262
  • 1
  • 13
  • 31