English 中文(简体)
用母体的子体(<a> tag s href Value [封闭]替换不符合同的圆面值
原标题:Replace non-conforming <img> tags src value using a substring from its parent <a> tag s href value [closed]
Closed. This question needs to be more focused. It is not currently accepting answers.

I am reading the contents of some files with file_get_contents the files contain a series of links and images. Ideally, they should all look like this. The link and image will always be the same matching 8 digit numbers like this...

<a href= /item/33333333 >
<img src="https://www.example.com/33333333.gif"></a>

<a href= /item/12345678 >
<img src="https://www.example.com/12345678.gif"></a>

有时,错误形象将显示,而不是 g。 此处注明该错误的档案内容......

<a href= /item/77778888 >
<img src="/images/default.svg"></a>

<a href= /item/33333333 >
<img src="https://www.example.com/33333333.gif"></a>

<a href= /item/12345678 >
<img src="https://www.example.com/12345678.gif"></a>

<a href= /item/22225555 >
<img src="/images/default.svg"></a>

因此,我需要最终结果。

<a href= /item/77778888 >
<img src="https://www.example.com/77778888.gif"></a>

<a href= /item/33333333 >
<img src="https://www.example.com/33333333.gif"></a>

<a href= /item/12345678 >
<img src="https://www.example.com/12345678.gif"></a>

<a href= /item/22225555 >
<img src="https://www.example.com/22225555.gif"></a>

I m not sure how to go about figuring out what 8 digit number appears before each default.svg and replacing it. Would we have to look back a certain number of characters each time default.svg appears to get that number?

增 编

问题回答

如果该档案是.php,那么它可能要存放一些变数的回路,如Rravel或 yii2 (home_url = HOME),或者使之难以编码。

最后应:

// Define variables
$itemNumber =  77778888 ;
$imageUrl =  https://www.example.com/  . $itemNumber .  .gif ;
?>
<a href= /item/<?php echo $itemNumber; ?> >
    <img src="<?php echo $imageUrl; ?>">
</a>```

类似于

if (preg_match_all( @<as+href= /item/(d{8}) >s+<img@ , $fileContent, $matches)) {
    foreach ($matches[1] as $id) {
        echo "<a href= /item/$id >
<img src= https://www.example.com/$id.gif ></a>

";
}

但我担心这一形象<代码>https://www.example.com/777788.gif。 不可能





相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

image changed but appears the same in browser

I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签