为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

PHP
阿晨1998 2023-04-28 14:45:45
<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 有人可以帮我怎么做吗?我搞不清楚了。我需要帮助。
查看完整描述

2 回答

?
largeQ

TA贡献2039条经验 获得超7个赞

如果它是字符串中的最后一个键/值对,则可以使用 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 target

foreach($ex as $key => $value){

  //--> Since we exploded at the `=` operator, we know the target string is `&hashValue` iterate through array and find that key/value pair

  if($value === '&hashValue'){


    //--> We know the hash is the next value in the array, so use the current key + 1 to find the hash

    $targetKey = $key + 1;

    //--> The next step is not needed if you do not have double quotes after or before the hash.

    //--> Simply use the targetKey to get the value of the hash --> $ex[$targetKey]

    //--> The following only applies if there is a trailing double quote left on the hash

    //--> If you have 6a1562f5c4901522ab6926a4caf9f278" left over

    $hash = explode('"', $ex[$targetKey]);

    $targetHash = $hash[0];

    echo $targetHash;

  }

}

退货:


6a1562f5c4901522ab6926a4caf9f278


现在你可以在任何你喜欢的地方复制 $targetHash 中的散列值......


如果您想更好地理解它是如何工作的,只需在每个步骤后添加一个 print_r() 并查看爆炸如何返回每个步骤的信息......


查看完整回答
反对 回复 2023-04-28
?
倚天杖

TA贡献1828条经验 获得超3个赞

$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">';


     $preg='/<input .*?hashValue=(.*?)".*?>/'; //Regular expression

     preg_match_all($preg,$str,$array);  //Match regular expression

     echo $array[1][0]; //return string


查看完整回答
反对 回复 2023-04-28
  • 2 回答
  • 0 关注
  • 99 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信