获取html输入元素的值作为php字符串

编程入门 行业动态 更新时间:2024-10-27 04:28:52
本文介绍了获取html输入元素的值作为php字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在php中有一个作为字符串加载的html文件,我需要获取HTML字符串中输入元素的值.有人可以帮我建立一个使用输入元素名称并返回其值的函数吗?

I have a html file loaded as a string in php, and I need to get the values of the input elements in the HTML string. Can someone help me build a function which takes the name of the input element and returns its value?

这是我要执行的功能的示例:

This is an example of the function I would like to do:

function getVal($name){ $htmlStr = "<form action = \"action.php\"><input type=\"hidden\" name=\"command\" value=\"123456\"> <input type=\"hidden\" name=\"quantity\" value=\"1\"> <input type=\"hidden\" name=\"user_mode\" value=\"1\"> <input type=\"hidden\" name=\"stock\" value=\"-1255303070\"> <input type=\"hidden\" name=\"id\" value="429762082"> <input type=\"hidden\" name=\"pidm\" value=\"2\"></form>"; // I'd like to get the value of $name here probably using preg_match return $value; //returns 123456 }

$ val = getVal("command"); //val应该为123456.

$val = getVal("command"); //val should be 123456.

任何伊达斯?谢谢

推荐答案

以下是DOM的示例:

$html = "<form action = \"action.php\"> <input type=\"hidden\" name=\"command\" value=\"123456\"> <input type=\"hidden\" name=\"quantity\" value=\"1\"> <input type=\"hidden\" name=\"user_mode\" value=\"1\"> <input type=\"hidden\" name=\"stock\" value=\"-1255303070\"> <input type=\"hidden\" name=\"id\" value=\"429762082\"> <input type=\"hidden\" name=\"pidm\" value=\"2\"> </form>"; $document = new DOMDocument(); $document->loadHTML($html); $inputs = $document->getElementsByTagName("input"); foreach ($inputs as $input) { if ($input->getAttribute("name") == "id") { $value = $input->getAttribute("value"); } } echo $value;

更多推荐

获取html输入元素的值作为php字符串

本文发布于:2023-10-10 11:28:27,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1478538.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   元素   html   php

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!