dpmfur2635 2015-08-10 14:35
浏览 22
已采纳

PHP之间替换字符串

I have the following string:

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

And I want to replace everything between src="cid: and " to a normal img url, so it becomes like this:

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

Everything before and after has to be preserved because it's a whole message, so it's also important to replace only between src="cid:REPLACE@ID"

I have tried this but this does nothing:

$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;

I have this code from: Replacing text between two limts

The $imgUrl is being build up correcly but preg_replace() isn't doing anything.

Anyone who knows a solution? Thanks in advance!!!

  • 写回答

1条回答 默认 最新

  • dongsha7215 2015-08-10 14:56
    关注

    Solved it by using this code:

    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;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端