Php 从imgsrc中删除http:

Php 从imgsrc中删除http:,php,http,src,Php,Http,Src,使用php是否可以从img src中删除http:protocol 因此,img src将: <img src="//www.example.com/image.jpg" /> 我只是不知道如何定义$filter。假设$filter工作正常并且正确获取了源代码,您也可以使用正则表达式替换: $contentImg = preg_replace('/^https?:/','', $string); “/^https?”:/”是一个正则表达式: -^字符表示字符串的开头,因此只能删除

使用php是否可以从img src中删除http:protocol

因此,img src将:

<img src="//www.example.com/image.jpg" />
我只是不知道如何定义$filter。

假设$filter工作正常并且正确获取了源代码,您也可以使用正则表达式替换:

$contentImg = preg_replace('/^https?:/','', $string);
“/^https?”:/”是一个正则表达式: -^字符表示字符串的开头,因此只能删除前面的潜在协议。 -那个?是一个特殊字符,指定s是可选的。因此,它将同时匹配http:和https:

使用正则表达式,可以编写更紧凑的查询。为了回答您也希望删除ftp和sftp的问题,您可以使用:

'/^(https?|s?ftp):/'
因为|表示或,括号用于分组

您还忘记删除冒号:

不过,我更担心的是,您的$filter将包含整个页面的源代码。在这种情况下,它弊大于利,因为包含http:的文本也可能被删除。为了解析和处理XML/HTML,最好使用DOMParser。这将引入一些开销,但正如一些软件工程师所争论的:软件工程是针对傻瓜设计系统,宇宙目前产生越来越多的傻瓜,因此少量额外开销是合理的

例如:

您肯定应该像前面所说的那样使用DOMParser,因为这种方法更安全:

$dom = new DOMDocument;
$dom->loadHTML($html); //$html is the input of the document
foreach ($dom->getElementsByTagName('img') as $image) {
    $image->setAttribute('src',preg_replace('/^https?:/','',$image->getAttribute('src')));
}
$html = $dom->saveHTML(); //html no stores the new version
在php-a中运行它可以为您的测试示例提供预期的输出

或在后处理步骤中:

$html = get_the_content();
$dom = new DOMDocument;
$dom->loadHTML($html); //$html is the input of the document
foreach ($dom->getElementsByTagName('img') as $image) {
    $image->setAttribute('src',preg_replace('/^https?:/','',$image->getAttribute('src')));
}
$html = $dom->saveHTML();
echo $html;
性能:

使用php-交互式shell 1'000'000实例对性能进行了测试:

$ php -a
php > $timea=microtime(true); for($i = 0; $i < 10000000; $i++) { str_replace(array('http:', 'https:'), '', 'http://www.google.com'); }; echo (microtime(true)-$timea);  echo "\n";
5.4192590713501
php > $timea=microtime(true); for($i = 0; $i < 10000000; $i++) { preg_replace('/^https?:/','', 'http://www.google.com'); }; echo (microtime(true)-$timea);  echo "\n";
5.986407995224
php > $timea=microtime(true); for($i = 0; $i < 10000000; $i++) { preg_replace('/https?:/','', 'http://www.google.com'); }; echo (microtime(true)-$timea);  echo "\n";
5.8694758415222
php > $timea=microtime(true); for($i = 0; $i < 10000000; $i++) { preg_replace('/(https?|s?ftp):/','', 'http://www.google.com'); }; echo (microtime(true)-$timea);  echo "\n";
6.0902049541473
php > $timea=microtime(true); for($i = 0; $i < 10000000; $i++) { str_replace(array('http:', 'https:','sftp:','ftp:'), '', 'http://www.google.com'); }; echo (microtime(true)-$timea);  echo "\n";
7.2881300449371
对于更多可能的部件,包括sftp和ftp:

是的,Stru_替换就是它的位置。它将是一个协议相对链接

<?php echo str_replace(array('http:', 'https:'), '', 'http://www.google.com'); ?>

果然如此。否则,您可以使用preg_replace,这将允许您使用正则表达式或正则表达式。CommuSoft给出了一个很好的例子作为答案。

$filter将是您的src字符串。这是从哪里来的?为什么要这样做?可能是协议相关链接。我遇到了这个和http+https混合服务器的问题1检查文档,2$filter将是您试图修改的任何文本,即您的HTML,3使用str_replace很简单,但它可能太简单了,也就是说,它会像这样破坏URLhttps://example.com/docs/http/tutorial.html@CommuSoft删除协议并离开//会告诉浏览器使用与源页面相同的协议请求静态文件。这个答案比CommuSoft的好,因为它使用str_replace,比preg_replace快得多。如果可以,请始终使用str_replace而不是正则表达式。@newz2000:正则表达式可以与输入在线性时间内匹配,而使用str_replace则取决于实现:如果它使用字符串匹配,则需要执行两个匹配http和https,因此对于更多协议,它不能很好地扩展。另一种方法是将其转换为正则表达式。。。此外,一个可能会成为问题的小方面是它可以替换HTTPS:在一个损坏的URL的中间。@ NeXZ2000:运行一些基准,我现在不知道你的定义快得多,但是这看起来很有竞争力,而且一旦匹配者的数量增长了,正则表达式可以获得更好的性能。请记住,这是一个非常简单的文本替换。如果事情更复杂的话,我可能不会发表这个声明。str_replace在这样简单的场景中通常会快6-20倍。@newz2000:在复杂的测试用例中,字符串的数量也会爆炸,并且said before stru replace也会与字符串的数量成线性关系。你能给出一个合理的测试用例,其中包含相关数量的字符串,比如说6+?。即使对于一个实例,也必须运行Knuth算法。Knuth的算法是正则表达式的一个特例,这样做确实更好,但时间复杂度是相同的,因此对于大型情况,时间复杂度是相同的。使用DOMParser,我将foreach$html->find'img[src]'作为元素-使用这个元素,我可以删除http:和https:using regex?@brandozz:Yes,但是请注意,您需要使用对dom解析器的正确调用来设置属性。将更新答案。当我从另一个文档中提取html时,DomParser会工作。我需要从与脚本位于同一页的图像中删除协议。如果我没做到,我很抱歉clear@brandozz:该脚本是终止脚本吗?你可以做的是编写一个.htaccess处理程序,对generate document.CommuSoft进行后期处理——实际上我正在使用WordPress,我想我可能已经找到了解决方案:$content=get\u the\u content$content=str_replacearray'http:'、'https:'、$content;echo$内容
$ php -a
php > $timea=microtime(true); for($i = 0; $i < 10000000; $i++) { str_replace(array('http:', 'https:'), '', 'http://www.google.com'); }; echo (microtime(true)-$timea);  echo "\n";
5.4192590713501
php > $timea=microtime(true); for($i = 0; $i < 10000000; $i++) { preg_replace('/^https?:/','', 'http://www.google.com'); }; echo (microtime(true)-$timea);  echo "\n";
5.986407995224
php > $timea=microtime(true); for($i = 0; $i < 10000000; $i++) { preg_replace('/https?:/','', 'http://www.google.com'); }; echo (microtime(true)-$timea);  echo "\n";
5.8694758415222
php > $timea=microtime(true); for($i = 0; $i < 10000000; $i++) { preg_replace('/(https?|s?ftp):/','', 'http://www.google.com'); }; echo (microtime(true)-$timea);  echo "\n";
6.0902049541473
php > $timea=microtime(true); for($i = 0; $i < 10000000; $i++) { str_replace(array('http:', 'https:','sftp:','ftp:'), '', 'http://www.google.com'); }; echo (microtime(true)-$timea);  echo "\n";
7.2881300449371
str_replace:           5.4193 s     0.0000054193 s/call
preg_replace (with ^): 5.9864 s     0.0000059864 s/call
preg_replace (no ^):   5.8695 s     0.0000058695 s/call
str_replace:           7.2881 s     0.0000072881 s/call
preg_replace (no ^):   6.0902 s     0.0000060902 s/call
<?php echo str_replace(array('http:', 'https:'), '', 'http://www.google.com'); ?>
//www.google.com