使用正则表达式从两个相对路径中的文件名

时间:2022-09-06 13:25:50

I would like to find fileName with extension in the text using regular expression(s). I have text like this:

我想使用正则表达式在文本中找到带扩展名的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>

I know that two relative paths, and I want to extract file names, that are situated in this relative path directory. I don't want files from another relative or absolute paths. There can be more occurences of these paths. Extensions may vary.

我知道两个相对路径,我想提取位于此相对路径目录中的文件名。我不想要来自其他相对路径或绝对路径的文件。这些路径可能会出现更多。扩展可能会有所不同。

I have tried this [\w-]+\.\w+, but it collides with style - width property, and it finds also file names from another relative paths in the text. Thanks for you help, in advance.

我试过这个[\ w - ] + \。\ w +,但是它与style-width属性相撞,它还从文本中的另一个相对路径中找到文件名。提前谢谢你的帮助。

Additional clarification: I want to find file names that lies in that two relative paths. I know paths, but I dont know which files are there. There can be multiple occurences.

补充说明:我想找到位于这两个相对路径中的文件名。我知道路径,但我不知道哪些文件存在。可能会有多次出现。

2 个解决方案

#1


1  

Try it:

 '/^[a-zA-Z0-9]+\.[a-zA-Z]{3,4}$/'    

Or maybe use basename()

或者也许使用basename()

#2


0  

The input is HTML so I would suggest a DOM solution:

输入是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));
}

Output:

string(8) "file.ext"
string(9) "file2.ext"

#1


1  

Try it:

 '/^[a-zA-Z0-9]+\.[a-zA-Z]{3,4}$/'    

Or maybe use basename()

或者也许使用basename()

#2


0  

The input is HTML so I would suggest a DOM solution:

输入是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));
}

Output:

string(8) "file.ext"
string(9) "file2.ext"