синтаксис preg_replace для замены имени хоста образа src новым именем хоста

Мне нужно синтаксис preg_replace, чтобы найти и заменить имя хоста img src с xyz.com на abc.host.prov.com
На мой взгляд, HTML-тег img выглядит так:

<img class="aligncenter wp-image-27283 size-large" src="http://example.com/wp-content/uploads/2017/04/image-3-1024x1024.jpeg" alt="homemade chocolate recipe" width="640" height="640" srcset="http://example.com/wp-content/uploads/2017/04/image-3-1024x1024.jpeg 1024w, http://example.com/wp-content/uploads/2017/04/image-3-300x300.jpeg 300w, http://example.com/wp-content/uploads/2017/04/image-3-768x768.jpeg 768w, http://example.com/wp-content/uploads/2017/04/image-3-696x696.jpeg 696w, http://example.com/wp-content/uploads/2017/04/image-3-1068x1068.jpeg 1068w, http://example.com/wp-content/uploads/2017/04/image-3-420x420.jpeg 420w, http://example.com/wp-content/uploads/2017/04/image-3-560x560.jpeg 560w, http://example.com/wp-content/uploads/2017/04/image-3.jpeg 1080w" sizes="(max-width: 640px) 100vw, 640px" />

и это должно быть заменено этим

<img class="aligncenter wp-image-27283 size-large" src="http://abc.host.prov.com/wp-content/uploads/2017/04/image-3-1024x1024.jpeg" alt="homemade chocolate recipe" width="640" height="640" srcset="http://abc.host.prov.com/wp-content/uploads/2017/04/image-3-1024x1024.jpeg 1024w, http://abc.host.prov.com/wp-content/uploads/2017/04/image-3-300x300.jpeg 300w, http://abc.host.prov.com/wp-content/uploads/2017/04/image-3-768x768.jpeg 768w, http://abc.host.prov.com/wp-content/uploads/2017/04/image-3-696x696.jpeg 696w, http://abc.host.prov.com/wp-content/uploads/2017/04/image-3-1068x1068.jpeg 1068w, http://abc.host.prov.com/wp-content/uploads/2017/04/image-3-420x420.jpeg 420w, http://abc.host.prov.com/wp-content/uploads/2017/04/image-3-560x560.jpeg 560w, http://abc.host.prov.com/wp-content/uploads/2017/04/image-3.jpeg 1080w" sizes="(max-width: 640px) 100vw, 640px" />

заранее спасибо

0

Решение

Вы должны использовать DOM для разбора HTML вместо Regex, Для разбора строки вы можете использовать Regex

Решение 1:

<?php

ini_set('display_errors', 1);
$string ='
<html>
<body>
<img class="aligncenter wp-image-27283 size-large" src="http://example.com/wp-content/uploads/2017/04/image-3-1024x1024.jpeg" alt="homemade chocolate recipe" width="640" height="640" srcset="http://example.com/wp-content/uploads/2017/04/image-3-1024x1024.jpeg 1024w, http://example.com/wp-content/uploads/2017/04/image-3-300x300.jpeg 300w, http://example.com/wp-content/uploads/2017/04/image-3-768x768.jpeg 768w, http://example.com/wp-content/uploads/2017/04/image-3-696x696.jpeg 696w, http://example.com/wp-content/uploads/2017/04/image-3-1068x1068.jpeg 1068w, http://example.com/wp-content/uploads/2017/04/image-3-420x420.jpeg 420w, http://example.com/wp-content/uploads/2017/04/image-3-560x560.jpeg 560w, http://example.com/wp-content/uploads/2017/04/image-3.jpeg 1080w" sizes="(max-width: 640px) 100vw, 640px" />
</body>
</html>';

$domObject= new DOMDocument();
$domObject->loadHTML($string);
$results=$domObject->getElementsByTagName("img");
foreach($results as $result)
{
$value=$result->getAttribute("src");
$value=preg_replace("/^\s*(https?:\/\/(?:www\.)?)[a-zA-Z0-9]+\.com/","$1abc.host.prov.com", $value);
$result->setAttribute("src",$value);
}
print_r($domObject->saveHTML());

Решение 2:
Попробуйте этот фрагмент кода здесь

<?php

ini_set('display_errors', 1);
$string ='
<html>
<body>
<img class="aligncenter wp-image-27283 size-large" src="http://example.com/wp-content/uploads/2017/04/image-3-1024x1024.jpeg" alt="homemade chocolate recipe" width="640" height="640" srcset="http://example.com/wp-content/uploads/2017/04/image-3-1024x1024.jpeg 1024w, http://example.com/wp-content/uploads/2017/04/image-3-300x300.jpeg 300w, http://example.com/wp-content/uploads/2017/04/image-3-768x768.jpeg 768w, http://example.com/wp-content/uploads/2017/04/image-3-696x696.jpeg 696w, http://example.com/wp-content/uploads/2017/04/image-3-1068x1068.jpeg 1068w, http://example.com/wp-content/uploads/2017/04/image-3-420x420.jpeg 420w, http://example.com/wp-content/uploads/2017/04/image-3-560x560.jpeg 560w, http://example.com/wp-content/uploads/2017/04/image-3.jpeg 1080w" sizes="(max-width: 640px) 100vw, 640px" />
</body>
</html>';
$domObject= new DOMDocument();
$domObject->loadHTML($string);
$domXpath= new DOMXPath($domObject);
$results=$domXpath->query("//img");
foreach($results as $result)
{
$value=$result->getAttribute("src");
$value=preg_replace("/^\s*(https?:\/\/(?:www\.)?)[a-zA-Z0-9]+\.com/","$1abc.host.prov.com", $value);
$result->setAttribute("src",$value);
}
print_r($domObject->saveHTML());
1

Другие решения

Вы можете попробовать это:

https?:\/\/\Kexample\.com

И заменить на это:

abc.host.prov.com

Regex Demo

Образец кода:

$re = '/https?:\/\/\Kexample\.com/';
$str='YOUR INPUT STRING GOES HERE';
$result = preg_replace($re, $subst, $str);
0

По вопросам рекламы [email protected]