简体   繁体   English

如何在没有img src的情况下删除http和https

[英]How to remove http and https without img src

code: 码:

$str = 'http://www.google.com <img src="http://placehold.it/350x150" />';
$str = preg_replace('/\b(https?):\/\/[-A-Z0-9+&@#\/%?=~_|$!:,.;]*[A-Z0-9+&@#\/%=~_|$]/i', '', $str);
echo $str;

output: 输出:

<img src="" />

i need this output: 我需要这个输出:

<img src="http://placehold.it/350x150" />

how can i do it? 我该怎么做?

thanks for help. 感谢帮助。

Your pattern 你的模式

/\b(https?):\/\/[-A-Z0-9+&@#\/%?=~_|$!:,.;]*[A-Z0-9+&@#\/%=~_|$]/i

removes all URLs (in the string) which start with protocol http or https . 删除所有以协议httphttps开头的URL (在字符串中) So when you apply it on your string, it will remove both that URL which is in the beginning of the string and that URL which is as src of <img> . 因此,当您将其应用于字符串时,它将删除位于字符串开头的URL和作为<img> src的URL。 So you have to use ^ in the beginning of your pattern: 所以你必须在模式的开头使用^

$str = 'http://www.google.com <img src="http://placehold.it/350x150" />';
$str = preg_replace('/^\b(https?):\/\/[-A-Z0-9+&@#\/%?=~_|$!:,.;]*[A-Z0-9+&@#\/%=~_|$]/i', '', $str);
echo $str;

Online Demo 在线演示


Or simply get what you need like this: 或者简单地得到你需要的东西:

/(<img.*\/>)/i

Online Demo 在线演示

I also think that DOMDocument and DOMXPath are preferable tools for parsing HTML markup. 我还认为DOMDocumentDOMXPath是解析HTML标记的首选工具。
But just in your particular case, here is solution with regexp negative lookbehind assertion : 但只是在你的特定情况下,这里是regexp 负向lookbehind断言的解决方案:

$str = 'http://www.google.com <img src="http://placehold.it/350x150" /> http://www.google.com.ua';

$str = preg_replace('/(?<!src=\")(https|http):\/\/[^\s]+\b/i', '', $str);

print_r($str);   // <img src="http://placehold.it/350x150" />

This will remove all urls excepting those which are inside an img src attribute 这将删除除img src属性内的所有URL之外的所有URL

Try: 尝试:

<[^>]*(*SKIP)(*FAIL)|\\b(https?):\\/\\/[-A-Z0-9+&@#\\/%?=~_|$!:,.;]*[A-Z0-9+&@#\\/%=~_|$]

The <[^>]* catches all the things within an unclosed < and (*SKIP)(*FAIL)| <[^>]*捕获未闭合的所有内容<(*SKIP)(*FAIL)| skips them. 跳过它们。

The rest is your regex. 剩下的就是你的正则表达式。

正则表达式:删除<img which src attribute is empty< div><div id="text_translate"><p> 我想要一个正则表达式模式来删除 src 属性为空的图像,例如:</p><pre> $html = '&lt;img src="adasas.jpg" /&gt;&lt;br /&gt;asasas&lt;br /&gt;sdfsdf&lt;br /&gt;&lt;img title="asa" src="" /&gt;';</pre><p> 或者</p><pre>$html = '&lt;img src="adasas.jpg" /&gt;&lt;br /&gt;asasas&lt;br /&gt;sdfsdf&lt;br /&gt;&lt;a href="adafgag"&gt;&lt;img title="asa" src="" /&gt;&lt;/a&gt;';</pre><p> 如果此&lt;img存在于&lt;a&gt;标记之间,我还想删除所有( &lt;a和&lt;img )。</p><p> 我测试了下面的代码,但它删除了所有 $html</p><pre> echo preg_replace( '?(&lt;a([^&gt;]+)&gt;).&lt;img(?*?)src=""([^&gt;]+)&gt;(&lt;/a&gt;),,si'; '' , $html );</pre><p> 任何人都可以帮助我吗?</p><p> 提前致谢</p></div> - RegEx : remove <img which src attribute is empty

暂无
暂无

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

相关问题 如果href或src的开头不是http,https或www,则PHP删除特定的标记或img标记 - PHP-Remove specific a tag or img tag if href or src does not start with http, https, or www 如何删除img src标签中的变量? - How to remove variable in img src tag? PHP 简单 HTML 如果路径中没有 http:,DOM 无法读取“data-src”或“img src” - PHP Simple HTML DOM can't read "data-src" or "img src" without http: in path 如何删除UTF-8字符串中带有SRC值的整个img元素? - how to remove whole img element with a SRC value in an UTF-8 string? 如何正确写作 <img src> 在PHP中没有转义为HTML - How to correctly write <img src> in php without escaping to HTML 将 src 组件链接为 https 而不是 http - linking src components as https not http 如何在空 src 或没有 src 时删除图像标签 - How to remove image tag when empty src or without src 删除不包含src属性的html中的img - Remove img in html that non contain src attribute 正则表达式:删除<img which src attribute is empty< div><div id="text_translate"><p> 我想要一个正则表达式模式来删除 src 属性为空的图像,例如:</p><pre> $html = '&lt;img src="adasas.jpg" /&gt;&lt;br /&gt;asasas&lt;br /&gt;sdfsdf&lt;br /&gt;&lt;img title="asa" src="" /&gt;';</pre><p> 或者</p><pre>$html = '&lt;img src="adasas.jpg" /&gt;&lt;br /&gt;asasas&lt;br /&gt;sdfsdf&lt;br /&gt;&lt;a href="adafgag"&gt;&lt;img title="asa" src="" /&gt;&lt;/a&gt;';</pre><p> 如果此&lt;img存在于&lt;a&gt;标记之间,我还想删除所有( &lt;a和&lt;img )。</p><p> 我测试了下面的代码,但它删除了所有 $html</p><pre> echo preg_replace( '?(&lt;a([^&gt;]+)&gt;).&lt;img(?*?)src=""([^&gt;]+)&gt;(&lt;/a&gt;),,si'; '' , $html );</pre><p> 任何人都可以帮助我吗?</p><p> 提前致谢</p></div> - RegEx : remove <img which src attribute is empty 删除所有属性<img> src 以外的标签 - Remove all attributes in <img> tag except src
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM