Reputation: 9
It's a far reach but I have been experimenting with both preg_match and preg_replace in an effort to do something extremely complicated.
I am trying to take an ENTIRE PayPal button code and extract only the 13-digit button id number. I have successfully used preg_replace to get all of the unwanted characters out and expose the code as one line of a jumbled mess. Unfortunately I extracted the equals signs too-which I assume I will need to extract a value= from this newly generated string.
The second issue I face, is even if I can leave the equal signs in the string I have created, there are two values in a paypal button form. Now, I somehow need to ignore the first value and grab only the second which is the id I need.
The only reason I am doing this is because PayPal doesn't seem to have a way to copy just that button id. Instead it generates the code for the whole form and when you click on it, it automatically highlights the whole form code not allowing you to simply highlight the small portion you want. I am having to do this to make it simpler for someone who doesn't understand code to copy the form code, paste it in a form field and have the website automatically grab that id number then use it in another script I have written.
So far, I have this:
<?php
$string = '';
$res = preg_replace("/[^a-zA-Z0-9]/", "", $string);
echo $res;
?>
When I add the PayPal form code to the string at the top - which looks like this:
<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="D3DPV3Y2WSA4J">
<input type="image" src="https://www.paypalobjects.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.paypalobjects.com/en_US/i/scr/pixel.gif" width="1" height="1">
</form>
I get this: formtargetpaypalactionhttpswwwpaypalcomcgibinwebscrmethodpostinputtypehiddennamecmdvaluesxclickinputtypehiddennamehostedbuttonidvalueD3DPV3Y2WSA4JinputtypeimagesrchttpswwwpaypalobjectscomenUSibtnbtncartLGgifborder0namesubmitaltPayPalThesafereasierwaytopayonlineimgaltborder0srchttpswwwpaypalobjectscomenUSiscrpixelgifwidth1height1form
And of course, the only thing I need from that is the second value: D3DPV3Y2WSA4J
I feel like I could do a little more research and figure out how to leave the equals signs in so I have the values all looking like: value= but I'm really not sure I will be able to skip the first value and grab only the second.
This is all a great learning experience for me - as I do not frequently use code like this but would love to gain more knowledge. Anyone who reads this, thank you so much for your time!
Upvotes: 0
Views: 88
Reputation: 3665
Good answer by Barmar. Another solution using preg_replace()
would be something like:
$res = preg_replace('~(.+)(name="hosted_button_id"\s+value=")([^"]+)(["].+)~s', '$3', $string);
echo $res;
Upvotes: 0
Reputation: 781028
The best solution is probably to use a DOM parser library like Simple DOM Parser. But you can do:
preg_match('/name="hosted_button_id"\s*value="(\w+)"/', $string, $match);
$button_id = $match[1];
Upvotes: 1