PHP之间替换字符串

时间:2015-08-10 14:35:19

标签: php

我有以下字符串:

$string = '<img id="Afbeelding_x0020_1" src="cid:image001.png@01D0D37B.5E4E6AE0" alt="logo" height="39" border="0" width="125">';

我想将src="cid:"之间的所有内容替换为普通的img url,因此它会变成这样:

$string = '<img id="Afbeelding_x0020_1" src="image.png" alt="logo" height="39" border="0" width="125">';

之前和之后的所有内容都必须保留,因为它是一条完整的信息,因此仅在src="cid:REPLACE@ID"

之间进行替换也很重要

我试过这个,但这没有做任何事情:

$query = "SELECT * FROM tickets WHERE id='".$ticketID."'";
            $con = $GLOBALS['db_con']->query($query);
            $rd = $con->fetch_assoc();
            if(strpos($rd['message'],'src="cid:') !== false){ $bodyImage = extract_unit($rd['message'], 'src="cid:', '@'); }
            $imgUrl = 'src="' . $GLOBALS['site_info']['url'] . '/tickets/' . $ticketID . '/' . $bodyImage . '"';
            $start = '\src="cid:';
            $end  = '\"';
            $result = preg_replace('#('.$start.')(.*)('.$end.')#si', $imgUrl, $rd['message']);
            return $result;

我的代码来自:Replacing text between two limts

正在积极构建$imgUrl,但preg_replace()并未做任何事情。

任何知道解决方案的人?在此先感谢!!!

1 个答案:

答案 0 :(得分:0)

使用此代码解决了这个问题:

function replace_ticketImage($msg,$ticketID,$replyID){
    if(strpos($msg,'src="cid:') !== false){
        $bodyString = extract_unit($msg, 'src="cid:', '"');
        $bodyImage = extract_unit($msg, 'src="cid:', '@');
        if($replyID < 1){
            $imgUrl = $GLOBALS['site_info']['url'] . '/attachments/tickets/' . $ticketID . '/' . $bodyImage;
        } else {
            $imgUrl = $GLOBALS['site_info']['url'] . '/attachments/tickets/' . $ticketID . '/' . $replyID . '/' . $bodyImage;
        }
        $result = preg_replace('/cid:'.$bodyString.'/', $imgUrl, $msg);
    } else {
        $result = $msg;
    }
    return $result;
}