Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用PHP和正则表达式提取Paypal按钮ID_Php_Html_Paypal - Fatal编程技术网

使用PHP和正则表达式提取Paypal按钮ID

使用PHP和正则表达式提取Paypal按钮ID,php,html,paypal,Php,Html,Paypal,我有以下Paypal“添加到购物车”HTML按钮代码: <form target="paypal" action="https://www.paypal.com/cgi-bin/webscr" method="post"> <input type="hidden" name="cmd" value="_s-xclick"> <input type="hidden" name="hosted_button_id" value="DJ445KDUWP402"> &

我有以下Paypal“添加到购物车”HTML按钮代码:

<form target="paypal" action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_s-xclick">
<input type="hidden" name="hosted_button_id" value="DJ445KDUWP402">
<input type="image" src="https://www.paypal.com/en_US/i/btn/btn_cart_LG.gif" border="0"   name="submit" alt="PayPal - The safer, easier way to pay online!">
<img alt="" border="0" src="https://www.paypal.com/en_US/i/scr/pixel.gif" width="1" height="1">
</form>

使用PHP和正则表达式,有人能告诉我如何获得“hosted_button_id”输入的值部分吗

谢谢

preg_match(“~~”,$html,$matches)
preg_match('~<input type="hidden" name="hosted_button_id" value="([0-9-A-Z]*?)">~', $html, $matches)
$matches[1]; //contains ID
$matches[1]//包含ID

应该做到这一点

这会奏效,而且非常灵活。它所需要的只是
value
属性出现在
hosted\u button\u id
元素中的
name
属性之后。
s
m
修饰符将允许您在多行上进行匹配。因此,您可以在本质上匹配整个html页面,而不仅仅是
hosted\u button\u id
元素

$str = '<form target="paypal" action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_s-xclick">
<input type="hidden" name="hosted_button_id" value="DJ445KDUWP402">
<input type="image" src="https://www.paypal.com/en_US/i/btn/btn_cart_LG.gif" border="0"   name="submit" alt="PayPal - The safer, easier way to pay online!">
<img alt="" border="0" src="https://www.paypal.com/en_US/i/scr/pixel.gif" width="1" height="1">
</form>';

preg_match('/name="hosted_button_id".*?value="(.*?)".*?>/ims', $str, $matches);
您可以进一步采用这种方法,并匹配所有输入元素的名称及其值

preg_match_all('/<input .*?name="(.*?)" .*?value="(.*?)".*?>/ims', $str, $matches);
print_r($matches); 
preg_match_all('//ims',$str,$matches);
打印(匹配项);
产出:

Array
(
    [0] => Array
        (
            [0] => <input type="hidden" name="cmd" value="_s-xclick">
            [1] => <input type="hidden" name="hosted_button_id" value="DJ445KDUWP402">
        )
    [1] => Array
        (
            [0] => cmd
            [1] => hosted_button_id
        )
    [2] => Array
        (
            [0] => _s-xclick
            [1] => DJ445KDUWP402
        )
)
数组
(
[0]=>阵列
(
[0] => 
[1] => 
)
[1] =>阵列
(
[0]=>cmd
[1] =>托管的按钮id
)
[2] =>阵列
(
[0]=>\u s-xclick
[1] =>DJ445KDUWP402
)
)

为什么要使用正则表达式?像SimpleXML这样的XML解析器会更加健壮。如果他不需要更健壮的东西,为什么要使用更健壮的东西呢?你不需要在数组中获取ID,而不是在值后面获取ID,$matches[1]不包含ID吗?是的,我只需要回显$matches[1]就可以得到我所要的值。
Array
(
    [0] => Array
        (
            [0] => <input type="hidden" name="cmd" value="_s-xclick">
            [1] => <input type="hidden" name="hosted_button_id" value="DJ445KDUWP402">
        )
    [1] => Array
        (
            [0] => cmd
            [1] => hosted_button_id
        )
    [2] => Array
        (
            [0] => _s-xclick
            [1] => DJ445KDUWP402
        )
)