title: PHP Complicated preg_match
body: <p>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.</p>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>So far, I have this:</p>
  
  <pre><code>&lt;?php
  $string = '';
  $res = preg_replace("/[^a-zA-Z0-9]/", "", $string);
  echo $res;
  ?&gt;
  </code></pre>
  
  <p>When I add the PayPal form code to the string at the top - which looks like this:</p>
  
  <pre><code>&lt;form target="paypal" action="https://www.paypal.com/cgi-bin/webscr" method="post"&gt;
  &lt;input type="hidden" name="cmd" value="_s-xclick"&gt;
  &lt;input type="hidden" name="hosted_button_id" value="D3DPV3Y2WSA4J"&gt;
  &lt;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!"&gt;
  &lt;img alt="" border="0" src="https://www.paypalobjects.com/en_US/i/scr/pixel.gif" width="1" height="1"&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>I get this:
      formtargetpaypalactionhttpswwwpaypalcomcgibinwebscrmethodpostinputtypehiddennamecmdvaluesxclickinputtypehiddennamehostedbuttonidvalueD3DPV3Y2WSA4JinputtypeimagesrchttpswwwpaypalobjectscomenUSibtnbtncartLGgifborder0namesubmitaltPayPalThesafereasierwaytopayonlineimgaltborder0srchttpswwwpaypalobjectscomenUSiscrpixelgifwidth1height1form</p>
  
  <p>And of course, the only thing I need from that is the second value: D3DPV3Y2WSA4J</p>
  
  <p>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.</p>
  
  <p>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!</p>