简体   繁体   English

php-如果在html文档中找到'cid:',则将值更改为src属性

[英]php - change value to src attribute if 'cid:' are found in html document

how can I tell php to look if a src attribute starts with 'cid:' and if yes, replace it with another value? 如何告诉php查看src属性是否以'cid:'开头,如果是,将其替换为另一个值?

<img src="cid:xx_xxxxxxxx_xxxxxxxxxxxxxxxxxxx" width="544" height="340">

<?php
    $new_link = "http://www.example.com'";
    preg_replace("#^cid:#", $new_link);
?>

The ^ is for the start of a string or line depending on the modifier being used. ^用于字符串或行的开头,具体取决于所使用的修饰符。 You want 你要

<?php
$new_link = "http://www.example.com'";
echo preg_replace('~src="cid.*?"~',"src='$new_link'", '<img src="cid:xx_xxxxxxxx_xxxxxxxxxxxxxxxxxxx" width="544" height="340">');
?>

Output: 输出:

<img src='http://www.example.com'' width="544" height="340">

The second trailing single quote at the end of the src is from the $newlink . src末尾的第二个单引号来自$newlink

The . . is for any character * for any number of "any characters" and the ? 代表任意字符*代表任意数量的“任意字符”,并且? says stop at the first occurrence of the next character. 说在下一个字符的第一次出现时停止。 In this case that is a double quote which should make this capture the whole string src attribute. 在这种情况下,这是双引号,应该使它捕获整个字符串src属性。

If the src can be using both single or double quotes this should account for that (also note i removed the trailing quote here from $newlink ). 如果src可以同时使用单引号或双引号,则这应予以考虑(还要注意,我从$newlink删除了尾随引号)。

<?php
$new_link = "http://www.example.com";
echo preg_replace('~src=("|\')cid.*?\1~',"src='$new_link'", '<img src="cid:xx_xxxxxxxx_xxxxxxxxxxxxxxxxxxx" width="544" height="340">');
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM