Backend\Modules\Blog\Actions\ImportWordpress::handleUrls PHP Метод

handleUrls() приватный Метод

We'll try and download images, and replace their urls We'll also check for links to schrijf.be and try to replace them
private handleUrls ( string $text, string $filter = '' ) : string
$text string The post text
$filter string The text that needs to be in a url before we start replacing it.
Результат string
    private function handleUrls($text, $filter = '')
    {
        // Check for images and download them, replace urls
        preg_match_all('/<img.*src="(.*)".*\\/>/Ui', $text, $matchesImages);
        if (isset($matchesImages[1]) && !empty($matchesImages[1])) {
            // Walk through image links
            foreach ($matchesImages[1] as $key => $file) {
                // Should we bother looking at this file?
                if (!empty($filter) && !mb_stristr($file, $filter)) {
                    continue;
                }
                $noSize = preg_replace('/\\-\\d+x\\d+/i', '', $file);
                if (isset($this->attachments[mb_strtolower($file)])) {
                    $text = str_replace($file, $this->attachments[mb_strtolower($file)], $text);
                } elseif (isset($this->attachments[mb_strtolower($noSize)])) {
                    $text = str_replace($file, $this->attachments[mb_strtolower($noSize)], $text);
                }
            }
        }
        // Check for links to schrijf.be and try to replace them
        preg_match_all('/<a.*href="(.*)".*\\/>/Ui', $text, $matchesLinks);
        if (isset($matchesLinks[1]) && !empty($matchesLinks[1])) {
            // Walk through links
            foreach ($matchesLinks[1] as $key => $link) {
                // Should we bother looking at this file?
                if (!empty($filter) && !mb_stristr($link, $filter)) {
                    continue;
                }
                $noSize = preg_replace('/\\-\\d+x\\d+/i', '', $link);
                if (isset($this->attachments[mb_strtolower($link)])) {
                    $text = str_replace($link, $this->attachments[mb_strtolower($link)], $text);
                } elseif (isset($this->attachments[mb_strtolower($noSize)])) {
                    $text = str_replace($link, $this->attachments[mb_strtolower($noSize)], $text);
                }
            }
        }
        return $text;
    }