来自字符串的 PHP 令牌
假设您有一个如下所示的字符串: 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以将
preg_match_all
与 PREG_OFFSET_CAPTURE 标志一起使用:然后您只需要替换
$matches[0]
中的项目,如下所示:You can use
preg_match_all
with the PREG_OFFSET_CAPTURE flag:Then you just need to replace the items in
$matches[0]
like this:在大多数方面,有一种更简单的方法。您将得到一个更基本的结果,但投入的工作量要少得多。
假设您有
tokena tokenb tokenc
存储在 $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 $dataNow 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.
您可以使用
explode()
,它会为您提供字符串中的标记数组,并使用strlen()
来计算字符串中的字符数。据我所知,我不认为有 PHP 函数可以告诉你元素在数组中的位置。要解决最后一个问题,您可以使用一个计数器变量来循环经过
explod()
数组(foreach()
forfor()
>) 并给出新数据中每个子数组的位置。如果我错了,请有人纠正我。
詹姆斯
You could use
explode()
, which will give you an array of tokens from the string, andstrlen()
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()
forfor()
) and gives each sub-array in the new data it's position.Someone please correct my if I'm wrong.
James
我最喜欢第一个答案 - 使用 PREG_OFFSET_CAPTURE。如果其他人感兴趣,我最终也写了一些这样做的东西,尽管我将接受第一个答案。
谢谢大家的帮助!
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!