正则表达式匹配插入符和破折号
Regex Match caret and dash in word
尝试使用 Leafly API 将 ^grape-ape
之类的字符串与 运行 搜索相匹配。我只是不擅长正则表达式,我的阅读障碍使我失去了模式。
知道为什么它似乎不是 matching/replacing 吗?
/*********************/
/* Tag Search Test */
/*********************/
$str = 'I really love that ^blue-dream and ^grape-ape';
preg_match('/^\^([a-z0-9\-])/i', $str, $match);
if (!(empty($match)) && $match != false) {
$slugs = $match;
if(!(is_array($slugs))) {
$strain = exec('curl -v -H "app_id:*******************" -H "app_key:*******************" -X GET "http://data.leafly.com/strains/'.$slugs.'"');
if (is_array(json_decode($strain))) {
$str = preg_replace('/^\^('.preg_quote($slugs).')/i', '<span title="'.$strain['descriptionPlain'].'">'.$strain['name'].'</span>', $str);
}
} else {
foreach($slugs as $slug) {
$strain = exec('curl -v -H "app_id:*******************" -H "app_key:*******************" -X GET "http://data.leafly.com/strains/'.$slug.'"');
if (is_array(json_decode($strain))) {
$str = preg_replace('/^\^('.preg_quote($slug).')/i', '<span title="'.$strain['descriptionPlain'].'">'.$strain['name'].'</span>', $str);
}
}
}
}
echo '<pre>';
print_r($str);
echo '</pre>';
/*********************/
/* Reference Test */
/*********************/
$strain = exec('curl -v -H "app_id:*******************" -H "app_key:*******************" -X GET "http://data.leafly.com/strains/grape-ape"');
echo '<pre>';
print_r(json_decode($strain));
echo '</pre>';
由于上一个 post 人在我试图让代码正常工作时出于某种原因删除了他们的答案,我想我会 post 答案。
我的问题实际上是在 API 请求的验证和 preg_match()
的执行中,应该是 preg_match_all()
/*********************/
/* Tag Search Test */
/*********************/
$str = 'I really love that ^blue-dream and ^grape-ape';
preg_match_all('/\^([a-z0-9\-]+)/i', $str, $match);
$i = 0;
foreach ( $match[1] as $slug ) {
$strain = (array) json_decode(exec("curl -v -H 'app_id:16d45ca2' -H 'app_key:9c699ac21615ef7a159cab290c1a6f2f' -X GET 'http://data.leafly.com/strains/$slug'"));
$str = str_replace($match[0][$i], '<span title="'.$strain['descriptionPlain'].'">'.$strain['name'].'</span>', $str);
$i++;
}
尝试使用 Leafly API 将 ^grape-ape
之类的字符串与 运行 搜索相匹配。我只是不擅长正则表达式,我的阅读障碍使我失去了模式。
知道为什么它似乎不是 matching/replacing 吗?
/*********************/
/* Tag Search Test */
/*********************/
$str = 'I really love that ^blue-dream and ^grape-ape';
preg_match('/^\^([a-z0-9\-])/i', $str, $match);
if (!(empty($match)) && $match != false) {
$slugs = $match;
if(!(is_array($slugs))) {
$strain = exec('curl -v -H "app_id:*******************" -H "app_key:*******************" -X GET "http://data.leafly.com/strains/'.$slugs.'"');
if (is_array(json_decode($strain))) {
$str = preg_replace('/^\^('.preg_quote($slugs).')/i', '<span title="'.$strain['descriptionPlain'].'">'.$strain['name'].'</span>', $str);
}
} else {
foreach($slugs as $slug) {
$strain = exec('curl -v -H "app_id:*******************" -H "app_key:*******************" -X GET "http://data.leafly.com/strains/'.$slug.'"');
if (is_array(json_decode($strain))) {
$str = preg_replace('/^\^('.preg_quote($slug).')/i', '<span title="'.$strain['descriptionPlain'].'">'.$strain['name'].'</span>', $str);
}
}
}
}
echo '<pre>';
print_r($str);
echo '</pre>';
/*********************/
/* Reference Test */
/*********************/
$strain = exec('curl -v -H "app_id:*******************" -H "app_key:*******************" -X GET "http://data.leafly.com/strains/grape-ape"');
echo '<pre>';
print_r(json_decode($strain));
echo '</pre>';
由于上一个 post 人在我试图让代码正常工作时出于某种原因删除了他们的答案,我想我会 post 答案。
我的问题实际上是在 API 请求的验证和 preg_match()
的执行中,应该是 preg_match_all()
/*********************/
/* Tag Search Test */
/*********************/
$str = 'I really love that ^blue-dream and ^grape-ape';
preg_match_all('/\^([a-z0-9\-]+)/i', $str, $match);
$i = 0;
foreach ( $match[1] as $slug ) {
$strain = (array) json_decode(exec("curl -v -H 'app_id:16d45ca2' -H 'app_key:9c699ac21615ef7a159cab290c1a6f2f' -X GET 'http://data.leafly.com/strains/$slug'"));
$str = str_replace($match[0][$i], '<span title="'.$strain['descriptionPlain'].'">'.$strain['name'].'</span>', $str);
$i++;
}