0

リンクが画像への直接リンクである場合に画像を表示できるように、関数にいくつかのコードを追加しました。唯一の問題は、URL が Web サイトへのリンクであり、実際の画像ではないため、同じ投稿の下にある他のすべてのリンクが壊れた画像を表示していることです。リンクが画像リンクまたはURLリンクであるかどうかを判断するために、画像の周りにifステートメントを実行する必要があるかどうかはわかりません。

別の関数を実行する可能性があると考えていたので、最初に parse を使用して画像リンクを別の関数に誘導し、画像を出力します。しかし、これが機能するかどうかはわかりません。

 $cont = preg_replace_callback( '|^\s*(https?://[^\s"]+)\s*$|im', array( $this, 'autoembedimage_callback' ), $cont);

PHP

/**
* Passes on any unlinked URLs that are on their own line for potential embedding.
* @param string $contThe content to be searched.
* @return string Potentially modified $content.
*/

function parse( $cont) {
    $cont = preg_replace_callback( '|^\s*(https?://[^\s"]+)\s*$|im', array( $this, 'autoembed_callback' ), $cont);
    return $cont;
}



/**
* Callback function for {@link AutoEmbed::parse()}.
* @param array $match A regex match array.
* @return string The embed HTML on success, otherwise the original URL.
*/

function autoembed_callback($match) {
    $attr['discover'] = true;
    $return = $this->get_html( $match[1], $attr );
    $match[0] = preg_replace("/\[\/?(.*?)\]/", "", $match[0]);
    $match[0] = preg_replace("/\<\/?(.*?)\>/", "", $match[0]);
    $image_file = ''.$match[1].'';
    $embedded = '<img id="feed" src="'.$image_file.'"  onerror="this.style.display=\'none\';" />';

    $m = trim(strtolower($match[0]));
    $m = str_replace("http://", "", $m);
    $m = str_replace("https://", "", $m);
    $m = str_replace("ftp://", "", $m);
    $m = str_replace("www.", "", $m);

    if (strlen($m) > 25)    {
        $m = substr($m, 0, 25) . "...";
    }

    if($attr['discover'] = true) {
        $return = $this->get_html( $match[1], $attr );
    }

    return "<a href=\"$match[0]\" target=\"_blank\">$m</a>\n$return\n\n$embedded\n";
}
4

0 に答える 0