首頁 > 後端開發 > php教程 > 请教一下链接转换不是写在IMG标签内吗?

请教一下链接转换不是写在IMG标签内吗?

WBOY
發布: 2016-07-06 13:53:46
原創
960 人瀏覽過

<code>public function format($image_url, $res, $content) {
        $no_match = false;
        $attachment_id = $res['id'];
        $url_path = str_replace(basename($res['file']), '', $res['url']);
        $size = isset($res['sizes'][$this->format['size']]) ? $this->format['size'] : 'full';
        if ($size == 'full') {
            $src = $res['url'];
            $width = $res['width'];
            $height = $res['height'];
        } else {
            $src = $url_path . $res['sizes'][$size]['file'];
            $width = $res['sizes'][$size]['width'];
            $height = $res['sizes'][$size]['height'];
        }
        $pattern_image_url = $this->encode_pattern($image_url);
        $preg = false;
        if ($this->keep_outside_links=='no') {
            $pattern = '/<a><img  alt="请教一下链接转换不是写在IMG标签内吗?" >]*'.$pattern_image_url.'.*?>?]+a>/i';
            $preg = preg_match($pattern, $content, $matches);
            if ($preg) {
                if ( $this->save_outside_links == 'yes' ) {
                    if ( preg_match('/</a><a>]*href=\"(.*?)\".*?>/i', $matches[0], $match) ) {
                        $link = $match[1];
                        $description = '</a><a href="'.%24link.'" target="_blank" rel="nofollow">'.__('Original Link', 'qqworld_auto_save_images').'</a>';
                        $description = apply_filters('qqworld-auto-save-images-save-outsite-link', $description, $link);
                        $args = array(
                            'ID' => $attachment_id,
                            'post_content' => $description
                        );
                        wp_update_post($args);
                    }
                }
                $args = $this->set_img_metadata($matches[0], $attachment_id);
            }
        }
        if (!$preg) {
            $pattern = '/<img  alt="请教一下链接转换不是写在IMG标签内吗?" >]*'.$pattern_image_url.'.*?>/i';
            if ( preg_match($pattern, $content, $matches) ) {
                $args = $this->set_img_metadata($matches[0], $attachment_id);
            } else {
                $pattern = '/'.$pattern_image_url.'/i';
                $no_match = true;
            }
        }
        $alt = isset($args['alt']) ? ' alt="'.$args['alt'].'"' : '';
        $title = isset($args['title']) ? ' title="'.$args['title'].'"' : '';
        $align = $this->auto_caption == 'yes' ? '' : 'align'.$this->format_align_to.' ';
        $img = '<img  class="'.$align.'size-'.$size.' wp-image-'.$attachment_id.'" src="'.%24src.'"    style="max-width:90%".$width.'" height="'.$height.'" alt="请教一下链接转换不是写在IMG标签内吗?" >';
        $link_to = $this->keep_outside_links=='no' ? $this->format['link-to'] : 'none';
        switch ($link_to) {
            case 'none':
                $replace = $img; break;
            case 'file':
                $replace = '<a href="'.%24res%5B'url'%5D.'">'.$img.'</a>';
                break;
            case 'post':
                $replace = '<a href="'.get_permalink(%24attachment_id).'">'.$img.'</a>';
                break;
        }
        if ($no_match) $replace = $res['url'];
        else if ($this->auto_caption == 'yes') $replace = '[caption id="attachment_'.$attachment_id.'" align="align'.$this->format_align_to.'" width="'.$width.'"]' . $replace . ' ' . (isset($args['alt']) ? $args['alt'] : '') . '[/caption]';
        $replace .= str_replace( '[Attachment ID]', $res['id'], $this->additional_content['after'] );

        if ( $this->keep_outside_links=='yes' ) {
            $patt = '/<a><img  alt="请教一下链接转换不是写在IMG标签内吗?" >]*'.$pattern_image_url.'.*?>?]+a>/i';
            if ( preg_match($patt, $content, $match) ) {
                $string = $match[0];
                $pos = strpos($string, '>');
                $string = substr_replace($string, ' rel="nofollow">', $pos, 1);
                $content = preg_replace($patt, $string, $content);
            }
        }

        $content = preg_replace($pattern, $replace, $content);
        return $content;
    }</a></code>
登入後複製
登入後複製

以上是一个插件的代码,里面写了链接转换。
我想阻止它将原本的图片链接转换。请问应该怎么修改。不是很懂PHP。请教下各位前辈 谢谢

回复内容:

<code>public function format($image_url, $res, $content) {
        $no_match = false;
        $attachment_id = $res['id'];
        $url_path = str_replace(basename($res['file']), '', $res['url']);
        $size = isset($res['sizes'][$this->format['size']]) ? $this->format['size'] : 'full';
        if ($size == 'full') {
            $src = $res['url'];
            $width = $res['width'];
            $height = $res['height'];
        } else {
            $src = $url_path . $res['sizes'][$size]['file'];
            $width = $res['sizes'][$size]['width'];
            $height = $res['sizes'][$size]['height'];
        }
        $pattern_image_url = $this->encode_pattern($image_url);
        $preg = false;
        if ($this->keep_outside_links=='no') {
            $pattern = '/<a><img  alt="请教一下链接转换不是写在IMG标签内吗?" >]*'.$pattern_image_url.'.*?>?]+a>/i';
            $preg = preg_match($pattern, $content, $matches);
            if ($preg) {
                if ( $this->save_outside_links == 'yes' ) {
                    if ( preg_match('/</a><a>]*href=\"(.*?)\".*?>/i', $matches[0], $match) ) {
                        $link = $match[1];
                        $description = '</a><a href="'.%24link.'" target="_blank" rel="nofollow">'.__('Original Link', 'qqworld_auto_save_images').'</a>';
                        $description = apply_filters('qqworld-auto-save-images-save-outsite-link', $description, $link);
                        $args = array(
                            'ID' => $attachment_id,
                            'post_content' => $description
                        );
                        wp_update_post($args);
                    }
                }
                $args = $this->set_img_metadata($matches[0], $attachment_id);
            }
        }
        if (!$preg) {
            $pattern = '/<img  alt="请教一下链接转换不是写在IMG标签内吗?" >]*'.$pattern_image_url.'.*?>/i';
            if ( preg_match($pattern, $content, $matches) ) {
                $args = $this->set_img_metadata($matches[0], $attachment_id);
            } else {
                $pattern = '/'.$pattern_image_url.'/i';
                $no_match = true;
            }
        }
        $alt = isset($args['alt']) ? ' alt="'.$args['alt'].'"' : '';
        $title = isset($args['title']) ? ' title="'.$args['title'].'"' : '';
        $align = $this->auto_caption == 'yes' ? '' : 'align'.$this->format_align_to.' ';
        $img = '<img  class="'.$align.'size-'.$size.' wp-image-'.$attachment_id.'" src="'.%24src.'"    style="max-width:90%".$width.'" height="'.$height.'" alt="请教一下链接转换不是写在IMG标签内吗?" >';
        $link_to = $this->keep_outside_links=='no' ? $this->format['link-to'] : 'none';
        switch ($link_to) {
            case 'none':
                $replace = $img; break;
            case 'file':
                $replace = '<a href="'.%24res%5B'url'%5D.'">'.$img.'</a>';
                break;
            case 'post':
                $replace = '<a href="'.get_permalink(%24attachment_id).'">'.$img.'</a>';
                break;
        }
        if ($no_match) $replace = $res['url'];
        else if ($this->auto_caption == 'yes') $replace = '[caption id="attachment_'.$attachment_id.'" align="align'.$this->format_align_to.'" width="'.$width.'"]' . $replace . ' ' . (isset($args['alt']) ? $args['alt'] : '') . '[/caption]';
        $replace .= str_replace( '[Attachment ID]', $res['id'], $this->additional_content['after'] );

        if ( $this->keep_outside_links=='yes' ) {
            $patt = '/<a><img  alt="请教一下链接转换不是写在IMG标签内吗?" >]*'.$pattern_image_url.'.*?>?]+a>/i';
            if ( preg_match($patt, $content, $match) ) {
                $string = $match[0];
                $pos = strpos($string, '>');
                $string = substr_replace($string, ' rel="nofollow">', $pos, 1);
                $content = preg_replace($patt, $string, $content);
            }
        }

        $content = preg_replace($pattern, $replace, $content);
        return $content;
    }</a></code>
登入後複製
登入後複製

以上是一个插件的代码,里面写了链接转换。
我想阻止它将原本的图片链接转换。请问应该怎么修改。不是很懂PHP。请教下各位前辈 谢谢

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板