gpt4 book ai didi

php - 查找带有各种 html 标签的字符串 PHP

转载 作者:搜寻专家 更新时间:2023-10-31 21:25:23 25 4
gpt4 key购买 nike

{
"status" : "success",
"prices" : [
{
"market_hash_name" : "AK-47 | Aquamarine Revenge (Battle-Scarred)",
"price" : "9.77",
"created_at" : 1463806291
},
{
"market_hash_name" : "AK-47 | Aquamarine Revenge (Factory New)",
"price" : "38.51",
"created_at" : 1463822081
},
{
"market_hash_name" : "AK-47 | Aquamarine Revenge (Field-Tested)",
"price" : "17.8",
"created_at" : 1463811694
},
{
"market_hash_name" : "AK-47 | Aquamarine Revenge (Minimal Wear)",
"price" : "26.0",
"created_at" : 1463823062
},
{
"market_hash_name" : "AK-47 | Aquamarine Revenge (Well-Worn)",
"price" : "12.9",
"created_at" : 1463832713
},
{
"market_hash_name" : "AK-47 | Black Laminate (Battle-Scarred)",
"price" : "6.36",
"created_at" : 1463832092
},
{
"market_hash_name" : "AK-47 | Black Laminate (Factory New)",
"price" : "93.0",
"created_at" : 1463800517
},
]
}

我在尝试循环遍历 AK-47 的价格和 preg_match 时遇到以下问题 | Black Laminate (Factory New) 好像没找到

我使用了下面的代码

$data = json_decode($json,true);
$prices = $data['prices'];
echo '<pre>';
$item = 'AK-47 | Black Laminate (Factory New)';
foreach($prices as $items ){
foreach($items as $key=>$value){
if(preg_match("/".$item."/i",$value )){
echo $key."<br>";
echo $value."<br>";
}
}
}
echo '</pre>';

关于我做错了什么的任何想法

任何帮助将不胜感激

最佳答案

使用 preg_quote 函数对正则表达式模式中使用的字符串中的特殊字符进行转义:

$data = json_decode($json,true);
$prices = $data['prices'];

$item = 'AK-47 | Black Laminate (Factory New)';
foreach ($prices as $items ){
foreach ($items as $key => $value){
if (preg_match("/". preg_quote($item) ."/i", $value )) {
echo $key."<br>";
echo $value."<br>";
}
}
}

输出:

market_hash_name
AK-47 | Black Laminate (Factory New)

http://php.net/manual/en/function.preg-quote.php

关于php - 查找带有各种 html 标签的字符串 PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37374650/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com