修改其他两个字符串中的通配符字符串
Modify a wildcard string within two other strings
假设我有以下代码作为变量的值(我可以使用 JS 或 PHP):
<div class="container">
<h1>The Page Title</h1>
<img src="MyImage.JPG" class="graphic" />
<p>Some body copy...</p>
<img class="graphic" src="misc/SecondImage.JPG" />
</div>
有没有一种方法可以将图像文件名转换为小写,同时保留所有其他内容的大小写,然后将所有新代码重写到变量中?
如果解决方案涉及查找 src="*"
的所有实例(或者字符串 src="
和 "
之间的任何内容)并将它们转换为小写,那会很好(我我对我添加的任何未来元素的 src
值也被转换感到满意。
试试下面的代码
$('img').each(function(){
$(this).attr('src', $(this).attr('src').toLowerCase())
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div class="container">
<h1>The Page Title</h1>
<img src="MyImage.JPG" class="graphic" />
<p>Some body copy...</p>
<img class="graphic" src="misc/SecondImage.JPG" />
</div>
与 PHP 其中 $html
是您的 html 内容:
$dom = new DOMDocument;
$dom->loadHTML($html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
$imgNodeList = $dom->getElementsByTagName('img');
foreach ($imgNodeList as $imgNode) {
$imgNode->setAttribute('src', strtolower($imgNode->getAttribute('src')));
}
echo $dom->saveHTML();
如果您只需要在路径基础(文件名)上应用小写字母,您可以使用 DOM
和正则表达式:
$html = <<< STR
<div class="container">
<h1>The Page Title</h1>
<img src="MyImage.JPG" class="graphic" />
<p>Some body copy...</p>
<img class="graphic" src="misc/SecondImage.JPG" />
</div>
STR;
$dom = new DOMDocument;
$dom->loadHTML($html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
// Get all images
$images = $dom->getElementsByTagName('img');
foreach ($images as $img) {
$newSrc = preg_replace_callback('~[^/]+$~', function($match) {
// Convert file names to lowercase
return strtolower($match[0]);
}, $img->getAttribute('src'));
// Set new attribute value
$img->setAttribute('src', $newSrc);
}
// Save to original variable
$html = $dom->saveHTML();
echo $html;
的输出
<div class="container">
<h1>The Page Title</h1>
<img src="myimage.jpg" class="graphic">
<p>Some body copy...</p>
<img class="graphic" src="misc/secondimage.jpg">
</div>
假设我有以下代码作为变量的值(我可以使用 JS 或 PHP):
<div class="container">
<h1>The Page Title</h1>
<img src="MyImage.JPG" class="graphic" />
<p>Some body copy...</p>
<img class="graphic" src="misc/SecondImage.JPG" />
</div>
有没有一种方法可以将图像文件名转换为小写,同时保留所有其他内容的大小写,然后将所有新代码重写到变量中?
如果解决方案涉及查找 src="*"
的所有实例(或者字符串 src="
和 "
之间的任何内容)并将它们转换为小写,那会很好(我我对我添加的任何未来元素的 src
值也被转换感到满意。
试试下面的代码
$('img').each(function(){
$(this).attr('src', $(this).attr('src').toLowerCase())
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div class="container">
<h1>The Page Title</h1>
<img src="MyImage.JPG" class="graphic" />
<p>Some body copy...</p>
<img class="graphic" src="misc/SecondImage.JPG" />
</div>
与 PHP 其中 $html
是您的 html 内容:
$dom = new DOMDocument;
$dom->loadHTML($html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
$imgNodeList = $dom->getElementsByTagName('img');
foreach ($imgNodeList as $imgNode) {
$imgNode->setAttribute('src', strtolower($imgNode->getAttribute('src')));
}
echo $dom->saveHTML();
如果您只需要在路径基础(文件名)上应用小写字母,您可以使用 DOM
和正则表达式:
$html = <<< STR
<div class="container">
<h1>The Page Title</h1>
<img src="MyImage.JPG" class="graphic" />
<p>Some body copy...</p>
<img class="graphic" src="misc/SecondImage.JPG" />
</div>
STR;
$dom = new DOMDocument;
$dom->loadHTML($html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
// Get all images
$images = $dom->getElementsByTagName('img');
foreach ($images as $img) {
$newSrc = preg_replace_callback('~[^/]+$~', function($match) {
// Convert file names to lowercase
return strtolower($match[0]);
}, $img->getAttribute('src'));
// Set new attribute value
$img->setAttribute('src', $newSrc);
}
// Save to original variable
$html = $dom->saveHTML();
echo $html;
<div class="container">
<h1>The Page Title</h1>
<img src="myimage.jpg" class="graphic">
<p>Some body copy...</p>
<img class="graphic" src="misc/secondimage.jpg">
</div>