简体   繁体   English

如何使用php getStr获取html标记的“名称”属性?

[英]How to get “name” attribute of html tag using php getStr?

I'm trying to find a way to get the content of this HTML attribute "name", using PHP getStr, I can't get it to work anyhow, I have already searched, but I couldn't find find something that may help me. 我正在尝试使用PHP getStr来获取此HTML属性“名称”的内容的方法,无论如何我都无法使它正常工作,我已经进行了搜索,但是找不到任何可能有帮助的东西我。

<input id="9d6e793e-eed2-4095-860a-41ca7f89396b.subject" maxlength="50" name="9d6e793e-eed2-4095-860a-41ca7f89396b:subject" value="" tabindex="1" class="subject required field" type="text"/>

I want to get this value into a string: 我想将此值转换为字符串:

9d6e793e-eed2-4095-860a-41ca7f89396b:subject 9d6e793e-eed2-4095-860a-41ca7f89396b:主题

I can get the value of a tag like this one: 我可以得到像这样的标签的值:

<input type="hidden" name="message" value="1442814179635.Oz1LxjnxVCMMJ0QpV0wGLx4roEA="/>

With this code: 使用此代码:

getStr($b,'name="message" value="','"');

But I can't find a way to get the attribute name of the first one? 但是我找不到找到第一个属性名称的方法吗?

Use regular expressions in PHP. 在PHP中使用正则表达式。 This code should be helpful: 该代码应该会有所帮助:

<?php

$str  = '<input type="hidden" name="message" value="1442814179635.Oz1LxjnxVCMMJ0QpV0wGLx4roEA="/>';

//forward slashes are the start and end delimeters
//third parameter is the array we want to fill with matches
if (preg_match('/name="([^"]+)"/', $str, $m)) {
    print $m[1];   
} else {
   //preg_match returns the number of matches found, 
   //so if here didn't match pattern
}

Output: 输出:

message

Check the PHP DOMElement::getAttribute method. 检查PHP DOMElement :: getAttribute方法。 It's all in the manual 全部在手册中

Here you go: 干得好:

<?php
$html = '<input id="9d6e793e-eed2-4095-860a-41ca7f89396b.subject" maxlength="50" name="9d6e793e-eed2-4095-860a-41ca7f89396b:subject" value="" tabindex="1" class="subject required field" type="text"/>';
$doc = new DOMDocument;
$doc->loadHTML($html);
$elements = $doc->getElementsByTagName("input");
foreach($elements as $element){
   echo $element->getAttribute('name');
}
?>

This bit of code will do what you want: 这部分代码将满足您的要求:

    <?php

        $b = '<input id="9d6e793e-eed2-4095-860a-41ca7f89396b.subject" maxlength="50" name="9d6e793e-eed2-4095-860a-41ca7f89396b:subject" value="" tabindex="1" class="subject required field" type="text"/>';

        echo "\$b = $b\n";

        $rest = substr(strstr($b,'name="'),6);

        echo "\$rest = $rest\n";

        $name = strstr($rest,'"',true);

        echo "\$name = $name\n";

    ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM