gpt4 book ai didi

c - 需要检查查询字符串之前是否存在任何扩展名

转载 作者:太空宇宙 更新时间:2023-11-04 08:13:05 24 4
gpt4 key购买 nike

我已经编写了代码来检查查询字符串

Logic::if query string is "?" 应该从 query string 中删除所有字符并打印 vailid 网址

        char str[] = "http://john.org/test.mp4?iufjdlwle";
char *pch;
pch = strtok(str,"?");
printf("%s\n",pch);

输出:

  bash-3.2$ ./querystring
http://john.com/test.mp4

但是我还得再检查一个案例

  1. 仅当查询字符串之前存在任何扩展时才需要获取 URL
  2. 如果查询字符串前没有扩展名,则需要跳过。

我试过这种方法, 代码的延续

        char *final;
final = pch+(strlen(pch)-3);
printf("%s\n",final);

if(strcasecmp(p,"mp4"))
printf("falure case\n");
else
printf("Success case\n");

它将单独用于 .mp4 扩展。 如果我将 *.mpeg*.m3u8*.flv 作为扩展名,它将失败。

有人可以指导我如何解决这个问题并使其正常工作吗?

最佳答案

查询字符串是在问号 ? 之后开始的,很好。

您应该尝试定义什么是扩展。对我来说,这是在 url 的最后一个组件中的点 (.) 之后可能发生的情况,其中组件用斜杠 (/) 分隔

所以你应该这样做:

  • 首先删除可能的查询字符串包括初始?
  • 然后找到最后一个/
  • 然后找到最后一个 . 出现在最后一个 / 之后

如果找到一个,它就是扩展的起点。

所以假设 pch 包含没有任何查询字符串的 url,你可以这样做:

char * ix = strrchr(pch, '/');
if (ix == NULL) {
// an URL without / is rather weird, better report and abort
...
}
ix = strrchr(ix, '.');
if (ix == NULL) {
// no extension here: ignore the url
...
}
else {
// found an URL containing an extension: process it
// ix+1 points to the extension
...
}

关于c - 需要检查查询字符串之前是否存在任何扩展名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37744089/

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