来自字符串的 PHP 令牌

发布于 2024-10-02 09:16:23 字数 573 浏览 4 评论 0原文

假设您有一个如下所示的字符串: token1 token2 tok3

并且您想要获取所有标记(特别是空格之间的字符串),以及它们的位置(偏移量)和长度)。

所以我想要一个看起来像这样的结果:

array(
    array(
        'value'=>'token1'
        'offset'=>0
        'length'=>6
    ),
    array(
        'value'=>'token2'
        'offset'=>7
        'length'=>6
    ),
    array(
        'value'=>'tok3'
        'offset'=>14
        'length'=>4
    ),
)

我知道这可以通过简单地循环字符串的字符来完成,并且我可以简单地编写一个函数来执行此操作。

我想知道,PHP 是否有任何内置的东西可以有效地完成此操作或至少有助于部分完成此操作?

我正在寻求建议并感谢所提供的任何帮助。谢谢

Let's say you have a string that looks like this:
token1 token2 tok3

And you want to get all of the tokens (specifically the strings between the spaces), AND ALSO their position (offset) and length).

So I would want a result that looks something like this:

array(
    array(
        'value'=>'token1'
        'offset'=>0
        'length'=>6
    ),
    array(
        'value'=>'token2'
        'offset'=>7
        'length'=>6
    ),
    array(
        'value'=>'tok3'
        'offset'=>14
        'length'=>4
    ),
)

I know that this can be done by simply looping through the characters of the string and I can simply write a function to do this.

I am wondering, does PHP have anything built-in that will do this efficiently or at least help with part of this?

I am looking for suggestions and appreciate any help given. Thanks

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

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

发布评论

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

评论(4

攒一口袋星星 2024-10-09 09:16:23

您可以将 preg_match_allPREG_OFFSET_CAPTURE 标志一起使用:

$str = 'token1 token2 tok3';
preg_match_all('/\S+/', $str, $matches, PREG_OFFSET_CAPTURE);
var_dump($matches);

然后您只需要替换 $matches[0] 中的项目,如下所示:

function update($match) {
    return array( 'value' => $value[0], 'offset' => $value[1], 'length' => strlen($value[0]));
}   
array_map('update', $matches[0]);
var_dump($matches[0]);

You can use preg_match_all with the PREG_OFFSET_CAPTURE flag:

$str = 'token1 token2 tok3';
preg_match_all('/\S+/', $str, $matches, PREG_OFFSET_CAPTURE);
var_dump($matches);

Then you just need to replace the items in $matches[0] like this:

function update($match) {
    return array( 'value' => $value[0], 'offset' => $value[1], 'length' => strlen($value[0]));
}   
array_map('update', $matches[0]);
var_dump($matches[0]);
不喜欢何必死缠烂打 2024-10-09 09:16:23

在大多数方面,有一种更简单的方法。您将得到一个更基本的结果,但投入的工作量要少得多。

假设您有 tokena tokenb tokenc 存储在 $data 中

$tokens = explode(' ', $data);

现在您有一个由空格分隔的令牌数组。它们将按顺序排列,因此 $tokens[0] = tokena、$tokens[1] = tokenb 等。您可以通过执行 strlen($tokens[$index]) 轻松获取任何给定项目的长度; 如果您需要知道传递了多少个令牌,请使用 $token_count = count($tokens);

没有那么复杂,但几乎不需要任何工作即可获得它。

There's a simpler way, in most respects. You'll have a more basic result, but with much less work put in.

Assuming you have tokena tokenb tokenc stored in $data

$tokens = explode(' ', $data);

Now you have an array of tokens separated by spaces. They will be in order, so $tokens[0] = tokena, $tokens[1] = tokenb, etc. You can very easily get the length of any given item by doing strlen($tokens[$index]); If you need to know how many tokens you were passed, use $token_count = count($tokens);

Not as sophisticated, but next to no work to get it.

作业与我同在 2024-10-09 09:16:23

您可以使用explode(),它会为您提供字符串中的标记数组,并使用strlen()来计算字符串中的字符数。据我所知,我不认为有 PHP 函数可以告诉你元素在数组中的位置。

要解决最后一个问题,您可以使用一个计数器变量来循环经过 explod() 数组(foreach() for for() >) 并给出新数据中每个子数组的位置。

如果我错了,请有人纠正我。

詹姆斯

You could use explode(), which will give you an array of tokens from the string, and strlen() to count the number of characters in the string. As far as I know, I don't think there is a PHP function to tell you where an element is in an array.

To get around the last problem, you could use a counter variable that loops through the explod()ed array (foreach() for for()) and gives each sub-array in the new data it's position.

Someone please correct my if I'm wrong.

James

梦忆晨望 2024-10-09 09:16:23

我最喜欢第一个答案 - 使用 PREG_OFFSET_CAPTURE。如果其他人感兴趣,我最终也写了一些这样做的东西,尽管我将接受第一个答案。

谢谢大家的帮助!

function get_words($string) {
    $string_chars = str_split($string);

    $words = array();
    $curr_offset = 0;

    foreach($reduced_string_chars as $offset=>$char) {
        if ($char == ' ') {
            if ($length) $words[] = array('offset'=>$curr_offset,'length'=>$length,'value'=>implode($value_array));

            $curr_offset = $offset;
            $length = 0;
            $value_array = array();
        }
        else {
            $length++;
            $value_array[] = $char;
        }

    }

    return $words;
}

I like the first answer the most - to use PREG_OFFSET_CAPTURE. In case anyone else is interested, I ended up writing something that does this as well, although I am going to accept the first answer.

Thank you everybody for helping!

function get_words($string) {
    $string_chars = str_split($string);

    $words = array();
    $curr_offset = 0;

    foreach($reduced_string_chars as $offset=>$char) {
        if ($char == ' ') {
            if ($length) $words[] = array('offset'=>$curr_offset,'length'=>$length,'value'=>implode($value_array));

            $curr_offset = $offset;
            $length = 0;
            $value_array = array();
        }
        else {
            $length++;
            $value_array[] = $char;
        }

    }

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