Using PHP and Regex to Retrieve Paypal Button ID

I have the following Paypal "Add to Cart" code:

<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>

Using PHP and regular expressions, can someone tell me how can I get only a part of the value of the input "hosting_button_id"?

Thank!

+3
source share
2 answers
preg_match('~<input type="hidden" name="hosted_button_id" value="([0-9-A-Z]*?)">~', $html, $matches)
$matches[1]; //contains ID

gotta do the trick

+2
source

It will work and very flexible. All that is required is that the attribute valueappears after the attribute namein the element hosted_button_id. Modifiers sand mallow you to match multiple lines. That way you can essentially map the entire html page, not just the element hosted_button_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);

:

Array
(
    [0] => name="hosted_button_id" value="DJ445KDUWP402">
    [1] => DJ445KDUWP402
)

.

preg_match_all('/<input .*?name="(.*?)" .*?value="(.*?)".*?>/ims', $str, $matches);
print_r($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

Source: https://habr.com/ru/post/1794746/


All Articles