如何将特定文本从 html 属性复制到 PHP 字符串?

<input type="hidden" name="payFormParams" id="payFormParams" value="payment_type=PaymentForm&merchant_id=117589463&trnType=P&errorPage=%2Fscripts%2Fpayment%2Fpayment%2Easp&approvedPage=&declinedPage=&epe_client_found=false&trnLanguage=eng&shipping_method=&ref1=&ref2=&ref3=&ref4=&ref5=&shippingMethod=&deliveryEstimate=&ordTax1Price=&ordTax2Price=&ordItemPrice=0&ordShippingPrice=0&trnOrderNumber=51029&trnAmount=5%2E00&ordName=Surreal+View&ordEmailAddress=xx%40gmail%2Ecom&ordPhoneNumber=2039581030&ordAddress1=3501+Jack+Northrop+Ave&ordCity=Hawthorne&ordProvince=CA&ordPostalCode=90250&ordCountry=US&paymentMethod=CC&trnCardOwner=Suyash&trnCardCvd=xxx&cavBirthMonth=&cavBirthDay=&cavBirthYear=&cavSin=&paymentAction=&trnCardNumber=xxxxxxxxx0412300&trnExpMonth=xx&trnExpYear=xx&aDFinancingType=&aDPlanNumber=&aDGracePeriod=&aDTerm=&hashValue=6a1562f5c4901522ab6926a4caf9f278">

这是生成的代码,我制作了一个字符串 $payid,但它复制了整个代码。我只需要 hashValue=

$payid = getStr($html, '< input type="hidden" name="payFormParams" id="payFormParams" value="','">');

但我只想将 的价值复制hashValue到那$payid只是6a1562f5c4901522ab6926a4caf9f278 有人可以帮我怎么做吗?我搞不清楚了。我需要帮助。


阿晨1998
浏览 125回答 2
2回答

largeQ

如果它是字符串中的最后一个键/值对,则可以使用 explode 获取哈希值,方法如下...在线 PHP 编辑器:https://3v4l.org/kUrJs$html = '<input type="hidden" name="payFormParams" id="payFormParams" value="payment_type=PaymentForm&merchant_id=117589463&trnType=P&errorPage=%2Fscripts%2Fpayment%2Fpayment%2Easp&approvedPage=&declinedPage=&epe_client_found=false&trnLanguage=eng&shipping_method=&ref1=&ref2=&ref3=&ref4=&ref5=&shippingMethod=&deliveryEstimate=&ordTax1Price=&ordTax2Price=&ordItemPrice=0&ordShippingPrice=0&trnOrderNumber=51029&trnAmount=5%2E00&ordName=Surreal+View&ordEmailAddress=xx%40gmail%2Ecom&ordPhoneNumber=2039581030&ordAddress1=3501+Jack+Northrop+Ave&ordCity=Hawthorne&ordProvince=CA&ordPostalCode=90250&ordCountry=US&paymentMethod=CC&trnCardOwner=Suyash&trnCardCvd=xxx&cavBirthMonth=&cavBirthDay=&cavBirthYear=&cavSin=&paymentAction=&trnCardNumber=xxxxxxxxx0412300&trnExpMonth=xx&trnExpYear=xx&aDFinancingType=&aDPlanNumber=&aDGracePeriod=&aDTerm=&hashValue=6a1562f5c4901522ab6926a4caf9f278">';//Explode the input tag as a string at the `=` operators.$ex = explode('=', $html);// now use the count of the exploded array as a reference and subtract the keys by one to get the value of the hash.$target = $ex[count($ex) - 1];// This leaves you with a value that has the hash followed by a double quote and the closing tag for the input, explode again using the double quote$hash = explode('"', $target);// This leaves the hash and the symbols in our array, use the first key of 0 to get the value of the hash as you want it.$targetHash = $hash[0];退货:6a1562f5c4901522ab6926a4caf9f278如果您不确定散列在字符串中的位置,您可以使用 explode 方法并搜索 的键hash,知道 NEXT 值将是实际的散列......所以它看起来像下面这样......一旦按 展开字符串,您就有了要搜索的=值,您知道展开后的数组中的下一个值将是实际的哈希值。&hashValue所以我们运行一个 foreach 并寻找那个值,然后找到keyturned的键value,我们用它来搜索散列,即key + 1。这将为我们提供带双引号的散列,引号展开这些散列并获取散列。$html = '<input type="hidden" name="payFormParams" id="payFormParams" value="payment_type=PaymentForm&merchant_id=117589463&trnType=P&errorPage=%2Fscripts%2Fpayment%2Fpayment%2Easp&approvedPage=&declinedPage=&epe_client_found=false&trnLanguage=eng&shipping_method=&ref1=&ref2=&ref3=&ref4=&ref5=&shippingMethod=&deliveryEstimate=&ordTax1Price=&ordTax2Price=&ordItemPrice=0&ordShippingPrice=0&trnOrderNumber=51029&trnAmount=5%2E00&ordName=Surreal+View&ordEmailAddress=xx%40gmail%2Ecom&ordPhoneNumber=2039581030&ordAddress1=3501+Jack+Northrop+Ave&ordCity=Hawthorne&ordProvince=CA&ordPostalCode=90250&ordCountry=US&paymentMethod=CC&trnCardOwner=Suyash&trnCardCvd=xxx&cavBirthMonth=&cavBirthDay=&cavBirthYear=&cavSin=&paymentAction=&hashValue=6a1562f5c4901522ab6926a4caf9f278=&trnCardNumber=xxxxxxxxx0412300&trnExpMonth=xx&trnExpYear=xx&aDFinancingType=&aDPlanNumber=&aDGracePeriod=&aDTerm=>';//--> Explode at the `=` operator and create an array to hold the exploded values$ex = explode('=', $html);//--> Run a loop and iterate over the key/value pairs to search for our targetforeach($ex as $key => $value){&nbsp; //--> Since we exploded at the `=` operator, we know the target string is `&hashValue` iterate through array and find that key/value pair&nbsp; if($value === '&hashValue'){&nbsp; &nbsp; //--> We know the hash is the next value in the array, so use the current key + 1 to find the hash&nbsp; &nbsp; $targetKey = $key + 1;&nbsp; &nbsp; //--> The next step is not needed if you do not have double quotes after or before the hash.&nbsp; &nbsp; //--> Simply use the targetKey to get the value of the hash --> $ex[$targetKey]&nbsp; &nbsp; //--> The following only applies if there is a trailing double quote left on the hash&nbsp; &nbsp; //--> If you have 6a1562f5c4901522ab6926a4caf9f278" left over&nbsp; &nbsp; $hash = explode('"', $ex[$targetKey]);&nbsp; &nbsp; $targetHash = $hash[0];&nbsp; &nbsp; echo $targetHash;&nbsp; }}退货:6a1562f5c4901522ab6926a4caf9f278现在你可以在任何你喜欢的地方复制 $targetHash 中的散列值......如果您想更好地理解它是如何工作的,只需在每个步骤后添加一个 print_r() 并查看爆炸如何返回每个步骤的信息......

倚天杖

$str = '<input type="hidden" name="payFormParams" id="payFormParams" value="payment_type=PaymentForm&merchant_id=117589463&trnType=P&errorPage=%2Fscripts%2Fpayment%2Fpayment%2Easp&approvedPage=&declinedPage=&epe_client_found=false&trnLanguage=eng&shipping_method=&ref1=&ref2=&ref3=&ref4=&ref5=&shippingMethod=&deliveryEstimate=&ordTax1Price=&ordTax2Price=&ordItemPrice=0&ordShippingPrice=0&trnOrderNumber=51029&trnAmount=5%2E00&ordName=Surreal+View&ordEmailAddress=xx%40gmail%2Ecom&ordPhoneNumber=2039581030&ordAddress1=3501+Jack+Northrop+Ave&ordCity=Hawthorne&ordProvince=CA&ordPostalCode=90250&ordCountry=US&paymentMethod=CC&trnCardOwner=Suyash&trnCardCvd=xxx&cavBirthMonth=&cavBirthDay=&cavBirthYear=&cavSin=&paymentAction=&trnCardNumber=xxxxxxxxx0412300&trnExpMonth=xx&trnExpYear=xx&aDFinancingType=&aDPlanNumber=&aDGracePeriod=&aDTerm=&hashValue=6a1562f5c4901522ab6926a4caf9f278">';&nbsp; &nbsp; &nbsp;$preg='/<input .*?hashValue=(.*?)".*?>/'; //Regular expression&nbsp; &nbsp; &nbsp;preg_match_all($preg,$str,$array);&nbsp; //Match regular expression&nbsp; &nbsp; &nbsp;echo $array[1][0]; //return string
打开App,查看更多内容
随时随地看视频慕课网APP