PHP在网址末尾删除cid

时间:2019-07-08 21:22:59

标签: php substr strpos

我正在使用PHP来从电子邮件正文中获取cid图像。我想删除cid:ii_jv1bt7pm0cid:HeaderImage或网址末尾显示的所有内容。

当我尝试这样做时:

$cid = 'cid:'.substr($cid, 1, strlen($cid)-2);

它将在网址末尾显示cid,例如:

  

http://example.com/#inbox/u/?id=123456&attid=0.1&msgid=1630808059112201633&view=attachment&display=viewii_jv1bt7pm0

所以我想实现使其显示为这样:

http://example.com/#inbox/u/?id=123456&attid=0.1&msgid=1630808059112201633&view=attachment&display=view

这是$cid的输出:

cid:ii_jv1bt7pm0
cid:HeaderImage

我想删除字符串以将其替换为空字符串,以使这些字符串不会显示在网址末尾。

这是完整的代码:

<?php

require_once "Mail.php";
require_once('Mail/IMAPv2.php');
$username = 'myusername';
$password = 'mypassword';
$mailserver = '{imap.example.com:993/imap/ssl/novalidate-cert}'.$_POST['mailserver'];

$mailbox = imap_open($mailserver, $username, $password) or die("Can't connect: " . imap_last_error());
email_number = 'somerandomnumbers';
$attachment = getAttachment($mailbox, $email_number);

function getAttachment(&$mail, $email_number) {


    $attachments = $mail->attachments;
    $msgNo = trim($mail->headerInfo->Msgno);
    $key = "somekeys";
    $email_id = bin2hex(openssl_encrypt($email_number, 'AES-128-CBC', $key));
    $attach_id = 0;
    $inline_id = 0.1;
    $html = '';

    foreach ($attachments as $attachment) {
        $partNo = $attachment['part'];
        $filename = $attachment['filename'];
        $filename = htmlentities($filename);

        $cid = $attachment['id'];

        if (isset($cid)) {

            if (strpos($mail->htmlText, 'viewHeaderImage')) {
                $mail->htmlText = str_replace('viewHeaderImage', 'view', $mail->htmlText);
            }
            else {
                $cid = 'cid:'.substr($cid, 1, strlen($cid)-2);
            }

            if($filename == 'noname.gif') {
                $tempfile = '/u/?id='.$email_id.'&attid='.$inline_id.'&msgid=1630808059112201633&view=attachment&display=view';
            }
            else {
                $tempfile = '/u/?id='.$email_id.'&attid='.$inline_id.'&msgid=1630808059112201633&view=attachment&display=view';
            }
            $mail->htmlText = EmailEmbeddedLinkReplace($mail->htmlText, $cid, $tempfile);
        }
    }
}


function EmailEmbeddedLinkReplace($html, $cid, $link)
{
    // In $html locate src="cid:$cid" and replace with $link.
    $cid = 'cid:'.substr($cid, 1, strlen($cid)-2);
    $newHtml = str_replace($cid, $link, $html);
    return $newHtml;
}

我尝试使用$cid = '';,但不会有任何区别,因为cid仍会显示在网址末尾。

您能给我一个例子来说明如何删除cid:ii_jv1bt7pm0cid:HeaderImage或如何删除它,以使它们不会显示在网址末尾吗? / p>

3 个答案:

答案 0 :(得分:1)

抱歉,我不完全了解您问题的性质。但是,

$cid = "http://example.com/#inbox/u/?id=123456&attid=0.1&msgid=1630808059112201633&view=attachment&display=viewii_jv1bt7pm0";
$cid = substr($cid,0,strrpos($cid,"display=view")+12);
echo $cid;

在这段代码中,我使用strrpos查找了display=view的最后一个$cid,然后加上了display=view的长度,即12。从0-indexend-index的{​​{1}}的URL。

以上代码的输出为:

  

http://example.com/#inbox/u/?id=123456&attid=0.1&msgid=1630808059112201633&view=attachment&display=view

希望这会有所帮助

答案 1 :(得分:1)

根据您在EmailEmbeddedLinkReplace()中的评论,看来这是您需要的:

function EmailEmbeddedLinkReplace($html, $cid, $link)
{
    // In $html locate src="cid:$cid" and replace with $link.
    return str_replace('src="cid:'.$cid.'"', $link, $html);
}

答案 2 :(得分:0)

使用rtrim从字符串中删除最后一个str。

echo rtrim("http://example.com/#inbox/u/?id=123456&attid=0.1&msgid=1630808059112201633&view=attachment&display=viewii_jv1bt7pm0","ii_jv1bt7pm0");