如何获取 php 中第一个“-”和第三个“-”之间的所有内容?
How to fetch everything between the first "-" & third "-" in php?
我有一个漂亮的url link:
www.abcd.com/Topic-V-CNHVNTNCHUUINP-Gangulys-waving-jersey-at-Lords-or-Dhonis-six-to-win-the-world-cup---Which-was-the-biggest-winning-moment-in-Indian-cricket-history-Sourav-Ganguly-Mahendra-Singh-Dhoni
我只想获取 "V-CNHVNTNCHUUINP"。我写的代码不起作用:
$value = "www.abcd.com/Topic-V-CNHVNTNCHUUINP-Gangulys-waving-jersey-at-Lords-or-Dhonis-six-to-win-the-world-cup---Which-was-the-biggest-winning-moment-in-Indian-cricket-history-Sourav-Ganguly-Mahendra-Singh-Dhoni";
$newstr = substr($value, 0, strpos($value, '-', strpos($value, '-')+3));
$afterunderscore = substr($newstr, strpos($newstr, "-") + 1);
这很吸引我:V-CNHVNTNCHUUINP。
但是在使用 link 进行测试时:
www.abcd.com/Topic-HN-CNHVNTNCHWTSSW-Sharukh-Khan-or-Salman-Khan---Who-is-the-biggest-superstar-of-bollywood
我得到的结果是:HN。
目标: 获取 HN-CNHVNTNCHWTSSW 当 link 为 www.abcd.com/Topic-HN-CNHVNTNCHWTSSW-Sharukh-Khan-or-Salman-Khan---Who-is-the-biggest-superstar-of-bollywood
您可以使用正则表达式进行匹配:
$value = "www.abcd.com/Topic-HN-CNHVNTNCHWTSSW-Sharukh-Khan-or-Salman-Khan---Who-is-the-biggest-superstar-of-bollywood";
// will account for "V-CNHVNTNCHUUINP" and "HN-CNHVNTNCHWTSSW"
$pattern = "/Topic-([A-Z]+-[A-Z]+)-/";
$matches = [];
preg_match($pattern, $value, $matches);
if (isset($matches[1])) {
// we got a match
echo $matches[1]; // prints "HN-CNHVNTNCHWTSSW"
}
这将按预期工作:
函数 strposX
将在 $haystack
中出现 $needle
$number
的 return 位置。然后只需使用 substr
到 select 想要的字符串部分存储在 $value
变量中。
function strposX($haystack, $needle, $number) {
preg_match_all("/$needle/", utf8_decode($haystack), $matches, PREG_OFFSET_CAPTURE);
return $matches[0][$number-1][1];
}
$value = "www.abcd.com/Topic-HN-CNHVNTNCHWTSSW-Sharukh-Khan-or-Salman-Khan---Who-is-the-biggest-superstar-of-bollywood";
$start = min(strposX($value, '-', 1), strposX($value, '-', 3));
$length = abs(strposX($value, '-', 1) - strposX($value, '-', 3));
echo substr($value, $start + 1, $length - 1);
输出:
HN-CNHVNTNCHWTSSW
我有一个漂亮的url link: www.abcd.com/Topic-V-CNHVNTNCHUUINP-Gangulys-waving-jersey-at-Lords-or-Dhonis-six-to-win-the-world-cup---Which-was-the-biggest-winning-moment-in-Indian-cricket-history-Sourav-Ganguly-Mahendra-Singh-Dhoni
我只想获取 "V-CNHVNTNCHUUINP"。我写的代码不起作用:
$value = "www.abcd.com/Topic-V-CNHVNTNCHUUINP-Gangulys-waving-jersey-at-Lords-or-Dhonis-six-to-win-the-world-cup---Which-was-the-biggest-winning-moment-in-Indian-cricket-history-Sourav-Ganguly-Mahendra-Singh-Dhoni";
$newstr = substr($value, 0, strpos($value, '-', strpos($value, '-')+3));
$afterunderscore = substr($newstr, strpos($newstr, "-") + 1);
这很吸引我:V-CNHVNTNCHUUINP。
但是在使用 link 进行测试时: www.abcd.com/Topic-HN-CNHVNTNCHWTSSW-Sharukh-Khan-or-Salman-Khan---Who-is-the-biggest-superstar-of-bollywood
我得到的结果是:HN。
目标: 获取 HN-CNHVNTNCHWTSSW 当 link 为 www.abcd.com/Topic-HN-CNHVNTNCHWTSSW-Sharukh-Khan-or-Salman-Khan---Who-is-the-biggest-superstar-of-bollywood
您可以使用正则表达式进行匹配:
$value = "www.abcd.com/Topic-HN-CNHVNTNCHWTSSW-Sharukh-Khan-or-Salman-Khan---Who-is-the-biggest-superstar-of-bollywood";
// will account for "V-CNHVNTNCHUUINP" and "HN-CNHVNTNCHWTSSW"
$pattern = "/Topic-([A-Z]+-[A-Z]+)-/";
$matches = [];
preg_match($pattern, $value, $matches);
if (isset($matches[1])) {
// we got a match
echo $matches[1]; // prints "HN-CNHVNTNCHWTSSW"
}
这将按预期工作:
函数 strposX
将在 $haystack
中出现 $needle
$number
的 return 位置。然后只需使用 substr
到 select 想要的字符串部分存储在 $value
变量中。
function strposX($haystack, $needle, $number) {
preg_match_all("/$needle/", utf8_decode($haystack), $matches, PREG_OFFSET_CAPTURE);
return $matches[0][$number-1][1];
}
$value = "www.abcd.com/Topic-HN-CNHVNTNCHWTSSW-Sharukh-Khan-or-Salman-Khan---Who-is-the-biggest-superstar-of-bollywood";
$start = min(strposX($value, '-', 1), strposX($value, '-', 3));
$length = abs(strposX($value, '-', 1) - strposX($value, '-', 3));
echo substr($value, $start + 1, $length - 1);
输出:
HN-CNHVNTNCHWTSSW