PHP 删除 url 末尾的 cid

PHP Remove cid at the end of the url

我正在 PHP 从电子邮件正文中获取 cid 图片。我想删除 cid:ii_jv1bt7pm0cid:HeaderImage 或 url.

末尾显示的任何内容

当我尝试这个时:

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

会显示url结尾的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

我想删除字符串以将其替换为空字符串,这样这些字符串就不会显示在 url 的末尾。

完整代码如下:

<?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 仍会显示在 url.

的末尾

能否请您举例说明如何删除 cid:ii_jv1bt7pm0cid:HeaderImage 或删除它的任何内容,这样它们就不会显示在 url?

抱歉,我没有完全理解您问题的性质。然而,

$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找到$cid的最后一个display=view,然后加上display=view的长度,即12。最后我将 URL 从 0-index 修剪为 display=viewend-index

以上代码的输出为:

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

希望对您有所帮助

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

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

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

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