[英]How to find all class names or ids from a CSS file that contains a background image using Regular expression and PHP?
[英]File names from two relative paths using regular expression
我想使用正则表达式在文本中找到带有扩展名的fileName。 我有这样的文字:
<p>Some text<a href="/relativePath1/file.ext">link</a>.<p>SomeText<img
src="/relativePath2/file2.ext" style="width: 200.2px; height: 1141px;"></p>
我知道有两个相对路径,并且我想提取文件名 ,它们位于此相对路径目录中。 我不要来自其他相对路径或绝对路径的文件。 这些路径可能会更多。 扩展名可能会有所不同。
我已经尝试过[\\w-]+\\.\\w+
,但是它与style-width属性冲突,并且它还会从文本的另一个相对路径中查找文件名。 预先感谢您的帮助。
进一步说明:我想找到位于这两个相对路径中的文件名。 我知道路径,但是我不知道有哪些文件。 可能会发生多次。
输入的是HTML,因此我建议使用DOM解决方案:
$html = <<<EOF
<p>Some text<a href="/relativePath1/file.ext">link</a>.<p>SomeText<img
src="/relativePath2/file2.ext" style="width: 200.2px; height: 1141px;"></p>
EOF;
$doc = new DOMDocument();
$doc->loadHTML($html);
$selector = new DOMXPath($doc);
// select all src and href attributes
foreach($selector->query('//@href | //@src') as $url) {
// extract the filename from path using basename()
var_dump(basename($url->nodeValue));
}
输出:
string(8) "file.ext"
string(9) "file2.ext"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.