[英]How to get text(file names) present between anchor tags in PHP?
I've following strings with me in which the file name is present in between anchor tags: 我跟随着字符串,其中在锚标记之间存在文件名:
$test1 = test<div class="comment_attach_file">
<a class="comment_attach_file_link" href="http://52.1.47.143/feed/download/year_2015/month_04/file_3b701923a804ed6f28c61c4cdc0ebcb2.txt" >phase2 screen.txt</a><br>
<a class="comment_attach_file_link_dwl" href="http://52.1.47.143/feed/download/year_2015/month_04/file_3b701923a804ed6f28c61c4cdc0ebcb2.txt" >Download</a>
</div>;
$test2 = This is a holiday list.<div class="comment_attach_file">
<a class="comment_attach_file_link" href="http://52.1.47.143/feed/download/year_2015/month_04/file_2c96b997f03eefab317811e368731bb6.pdf" >Holiday List-2013.pdf</a><br>
<a class="comment_attach_file_link_dwl" href="http://52.1.47.143/feed/download/year_2015/month_04/file_2c96b997f03eefab317811e368731bb6.pdf" >Download</a>
</div>;
$test3 = <div class="comment_attach_file">
<a class="comment_attach_file_link" href="http://52.1.47.143/feed/download/year_2015/month_04/file_8479c0b60867fdce35ae94a668dfbba9.docx" >sample2.docx</a><br>
</div>;
From the first string I want text(ie file name) "phase2 screen.txt" 从第一个字符串中,我想要文本(即文件名) “ phase2 screen.txt”
From the second string I want text(ie file name) "Holiday List-2013.pdf" 在第二个字符串中,我想要文本(即文件名) “ Holiday List-2013.pdf”
From the third string I want text(ie file name) "sample2.docx" 从第三个字符串中,我想要文本(即文件名) “ sample2.docx”
How should I do in PHP using $dom = new DOMDocument;
我应该如何在PHP中使用
$dom = new DOMDocument;
? ?
Please someone help me. 请有人帮我。
Thanks. 谢谢。
If you want to get the value after the hash mark or anchor as shown in a user's browser: This isn't possible with "standard" HTTP as this value is never sent to the server (hence it won't be available in $_SERVER["REQUEST_URI"]
or similar predefined variables). 如果要在用户浏览器中显示哈希值或锚点之后的值:使用“标准” HTTP则无法实现,因为该值永远不会发送到服务器(因此在
$_SERVER["REQUEST_URI"]
中将不可用) $_SERVER["REQUEST_URI"]
或类似的预定义变量)。 You would need some sort of JavaScript magic on the client side, eg to include this value as a POST parameter. 您可能需要在客户端使用某种JavaScript魔术,例如,将该值包含为POST参数。
In dom you can use some function like this to get the links and change it according to your need 在dom中,您可以使用类似这样的功能来获取链接并根据需要进行更改
function findAnchors($html)
{
$links = array();
$doc = new DOMDocument();
$doc->loadHTML($html);
$navbars = $doc->getElementsByTagName('div');
foreach ($navbars as $navbar) {
$id = $navbar->getAttribute('id');
if ($id === "anchors") {
$anchors = $navbar->getElementsByTagName('a');
foreach ($anchors as $a) {
$links[] = $doc->saveHTML($a);
}
}
}
return $links;
}
You can use DOMxpath to target that link that contains the text you want, use its class to point to it: 您可以使用DOMxpath来定位包含所需文本的链接,使用其类指向它:
$dom = new DOMDocument;
for($i = 1; $i <= 3; $i++) {
@$dom->loadHTML(${"test{$i}"});
$xpath = new DOMXpath($dom);
$file_name = $xpath->evaluate('string(//a[@class="comment_attach_file_link"])');
echo $file_name , '<br/>';
}
Or if you don't want to use xpath, you can get the anchor elements and check for its class, if its that one, get the ->nodeValue
: 或者,如果您不想使用xpath,则可以获取锚元素并检查其类,如果是该类,则获取
->nodeValue
:
$dom = new DOMDocument;
for($i = 1; $i <= 3; $i++) {
@$dom->loadHTML(${"test{$i}"});
foreach($dom->getElementsByTagName('a') as $anchor) {
if($anchor->getAttribute('class') === 'comment_attach_file_link') {
echo $anchor->nodeValue, '<br/>';
break;
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.