strspn 有什么意义?

发布于 2024-07-11 04:01:06 字数 194 浏览 4 评论 0原文

今天在工作中,我们试图找出您使用 strspn 的任何原因。

我搜索了谷歌代码,看看它是否曾经以有用的方式实现过,但结果却是空白。 我只是无法想象我真的需要知道仅包含另一个字符串中的字符的字符串的第一段的长度的情况。 有任何想法吗?

At work today we were trying to come up with any reason you would use strspn.

I searched google code to see if it's ever been implemented in a useful way and came up blank. I just can't imagine a situation in which I would really need to know the length of the first segment of a string that contains only characters from another string. Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

清音悠歌 2024-07-18 04:01:07

我认为它非常适合列入黑名单并让用户知道错误从哪里开始。 就像 MySQL 从发生错误的地方返回部分查询一样。

请查看这个函数,它可以让用户知道他的评论的哪一部分无效:

function blacklistChars($yourComment){

$blacklistedChars = "!@#$%^&*()";
$validLength = strcspn($yourComment, $blacklistedChars);
if ($validLength !== strlen($yourComment))
{

    $error = "Your comment contains invalid chars starting from here: `" . 
        substr($yourComment, (int) '-' . $validLength) . "`";

    return $error;
}

return false;
}

$yourComment = "Hello, why can you not type and $ dollar sign in the text?"; 
$yourCommentError = blacklistChars($yourComment);

if ($yourCommentError <> false)
echo $yourCommentError;

I think its great for blacklisting and letting the user know from where the error started. Like MySQL returns part of the query from where the error occured.

Please see this function, that lets the user know which part of his comment is not valid:

function blacklistChars($yourComment){

$blacklistedChars = "!@#$%^&*()";
$validLength = strcspn($yourComment, $blacklistedChars);
if ($validLength !== strlen($yourComment))
{

    $error = "Your comment contains invalid chars starting from here: `" . 
        substr($yourComment, (int) '-' . $validLength) . "`";

    return $error;
}

return false;
}

$yourComment = "Hello, why can you not type and $ dollar sign in the text?"; 
$yourCommentError = blacklistChars($yourComment);

if ($yourCommentError <> false)
echo $yourCommentError;
司马昭之心 2024-07-18 04:01:07

您可以使用它来捕获初始缩进,例如标准化 Markdown 或 YAML 字符串:

<?php

$content = <<<TEXT
    aaa
    aaa
        aaa
        aaa
    aaa
    aaa
TEXT;

$content = trim($content, "\n");
$dent = strspn($content, ' ');

if ($dent > 0) {
    // Remove the first indent
    $content = substr($content, $dent);
    // Remove other indent(s) after the first line
    $content = strtr($content, ["\n" . str_repeat(' ', $dent) => "\n"]);
}

echo $content;

You can use it to capture the initial indent, for example, to normalize Markdown or YAML string:

<?php

$content = <<<TEXT
    aaa
    aaa
        aaa
        aaa
    aaa
    aaa
TEXT;

$content = trim($content, "\n");
$dent = strspn($content, ' ');

if ($dent > 0) {
    // Remove the first indent
    $content = substr($content, $dent);
    // Remove other indent(s) after the first line
    $content = strtr($content, ["\n" . str_repeat(' ', $dent) => "\n"]);
}

echo $content;
机场等船 2024-07-18 04:01:07

它对于像 atoi 这样的函数特别有用 - 你有一个字符串想要转换为数字,并且你不想处理任何不在集合“-.0123456789”中的东西

但是是的,它有限制使用。

-亚当

It is useful specificaly for functions like atoi - where you have a string you want to convert to a number, and you don't want to deal with anything that isn't in the set "-.0123456789"

But yes, it has limited use.

-Adam

灯角 2024-07-18 04:01:06

尽管您链接到 PHP 手册,但 strspn() 函数来自 C 库,以及 strlen()strcpy()、< code>strcmp() 等。

strspn() 是一个方便的替代方法,可以逐个字符地挑选字符串,测试字符是否与一组值中的一个匹配。 在编写标记器时它很有用。 strspn() 的替代方案是大量重复且容易出错的代码,如下所示:

for (p = stringbuf; *p; p++) {
  if (*p == 'a' || *p == 'b' || *p = 'c' ... || *p == 'z') {
    /* still parsing current token */
  }
}

你能发现错误吗? :-)

当然,在内置支持正则表达式匹配的语言中,strspn() 没有什么意义。 但是当用 C 语言编写 DSL 的基本解析器时,它非常漂亮。

Although you link to the PHP manual, the strspn() function comes from C libraries, along with strlen(), strcpy(), strcmp(), etc.

strspn() is a convenient alternative to picking through a string character by character, testing if the characters match one of a set of values. It's useful when writing tokenizers. The alternative to strspn() would be lots of repetitive and error-prone code like the following:

for (p = stringbuf; *p; p++) {
  if (*p == 'a' || *p == 'b' || *p = 'c' ... || *p == 'z') {
    /* still parsing current token */
  }
}

Can you spot the error? :-)

Of course in a language with builtin support for regular expression matching, strspn() makes little sense. But when writing a rudimentary parser for a DSL in C, it's pretty nifty.

白芷 2024-07-18 04:01:06

它基于 ANSI C 函数 strspn()。 它在没有高级字符串类的低级 C 解析代码中很有用。 它在 PHP 中的用处要小得多,因为 PHP 有很多有用的字符串解析函数。

It's based on the the ANSI C function strspn(). It can be useful in low-level C parsing code, where there is no high-level string class. It's considerably less useful in PHP, which has lots of useful string parsing functions.

赠佳期 2024-07-18 04:01:06

嗯,根据我的理解,它与这个正则表达式是一样的:

^[set]*

其中 set 是包含要查找的字符的字符串。

您可以使用它来搜索字符串开头的任何数字或文本并进行拆分。

看来将代码移植到 php 时很有用。

Well, by my understanding, its the same thing as this regex:

^[set]*

Where set is the string containing the characters to be found.

You could use it to search for any number or text at the beginning of a string and split.

It seems it would be useful when porting code to php.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文