使用 PHP 将字符串分成两半(单词感知)
我正在尝试将字符串分成两半,并且它不应该在单词中间拆分。
到目前为止,我想出了以下 99% 有效的方法:
$text = "The Quick : Brown Fox Jumped Over The Lazy / Dog";
$half = (int)ceil(count($words = str_word_count($text, 1)) / 2);
$string1 = implode(' ', array_slice($words, 0, $half));
$string2 = implode(' ', array_slice($words, $half));
这确实有效,根据字符串中的单词数正确地将任何字符串分成两半。但是,它会删除字符串中的任何符号,例如,对于上面的示例,它将输出:
The Quick Brown Fox Jumped
Over The Lazy Dog
我需要在分割后保留字符串中的所有符号,例如:和/。我不明白为什么当前的代码要删除符号...如果您可以提供替代方法或修复此方法以不删除符号,我们将不胜感激:)
I'm trying to split strings in half, and it should not split in the middle of a word.
So far I came up with the following which is 99% working :
$text = "The Quick : Brown Fox Jumped Over The Lazy / Dog";
$half = (int)ceil(count($words = str_word_count($text, 1)) / 2);
$string1 = implode(' ', array_slice($words, 0, $half));
$string2 = implode(' ', array_slice($words, $half));
This does work, correctly splitting any string in half according to the number of words in the string. However, it is removing any symbols in the string, for example for the above example it would output :
The Quick Brown Fox Jumped
Over The Lazy Dog
I need to keep all the symbols like : and / in the string after being split. I don't understand why the current code is removing the symbols... If you can provide an alternative method or fix this method to not remove symbols, it would be greatly appreciated :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
在查看您的示例输出后,我注意到我们所有的示例都已关闭,如果字符串的中间位于单词内,我们会向 string1 提供较少的值,而不是提供更多的值。
例如,
The Quick : Brown Fox Jumped Over The Lazy / Dog
的中间是The Quick : Brown Fox Ju
,它位于单词的中间,第一个示例给出string2 分割词;下面的例子给出了 string1 的分割词。在分割词上给予较少的字符串1
在分割词上给予字符串1更多
Upon looking at your example output, I noticed all our examples are off, we're giving less to string1 if the middle of the string is inside a word rather then giving more.
For example the middle of
The Quick : Brown Fox Jumped Over The Lazy / Dog
isThe Quick : Brown Fox Ju
which is in the middle of a word, this first example gives string2 the split word; the bottom example gives string1 the split word.Give less to string1 on split word
Give more to string1 on split word
演示:http://codepad.viper-7.com/I58gcI
Demo: http://codepad.viper-7.com/I58gcI
我知道这是一个老问题,但我有下面的代码应该可以完成所需的操作。
默认情况下,它会在中间之后第一次出现空格时分割字符串。
如果中间之后没有空格,则查找中间之前的最后一个空格。
这些是示例:
示例 1:
示例 2:
示例 3:
示例 4:
希望这可以帮助那里的人:)
I know this is an old question, but I have the following piece of code that should do what is needed.
It by default it splits the string on the first occurrence of a space after the middle.
If there are no spaces after the middle, it looks for the last space before the middle.
These are examples:
Example 1:
Example 2:
Example 3:
Example 4:
Hope this can help someone out there :)
使用此函数将字符串拆分为半字。
Use this function to split string into half words..
与往常一样,正则表达式使开发人员免于大量繁琐的字符串操作调用和不必要的脚本膨胀。我什至会大胆地说,正则表达式模式可能比充满字符串函数的脚本更容易理解。
代码:(Demo)
输出:
实际上,我们通过计算字符串的字符数来计算字符串的中心,然后除以两个并删除所有小数位。
然后贪婪地匹配零到
$halfWay
个字符,然后用\K
忘记这些字符,然后在最新的限定空间上分割字符串。preg_split()
的第三个参数决定了可以生成的最大元素数量。如果任务是将句子拆分为包含相同数量单词的两部分,则以下是如何计算单词数并使用正则表达式进行拆分:(演示)
输出:
As usual, regex spares the developer a lot of tedious string manipulation calls and unnecessary script bloat. I'll even go out on a limb and say that the regex pattern might be easier to understand than the string function laden scripts.
Code: (Demo)
Output:
Effectively, we calculate the center of the string by counting its characters, then dividing by two and removing any decimal places.
Then greedily match zero to
$halfWay
number of characters, then forget those characters with\K
, then split the string on the latest qualifying space. The 3rd parameter ofpreg_split()
determines the maximum amount of elements which may be produced.If the task is meant to split the sentence into two parts containing the same number of words, then this is how to count the words and use a regex to split: (Demo)
Output:
我创建了一个很好的解决方案,我们不会丢失字符或单词不会被错误地剪切。
I was created a great solution, where we dont lost characters or Where the word is not cut wrongly.
我尝试使用其中的几个答案,但没有得到最好的结果,所以我想我会分享解决方案。我想将标题分成两半并显示一半白色和一半绿色。
我最终将字数、文本长度、中间点和字符串的三分之一结合起来。这使我能够确保白色部分位于第三路/中路标记之间。
我希望它能帮助某人。请注意我的代码 -,&等将被视为一个词 - 它在我的测试中没有给我带来问题,但如果我发现它没有像我希望的那样工作,我会更新它。
I tried using a couple of these answers but didn't get the best results so I thought I would share the solution. I wanted to split my titles in half and display one half white and one half green.
I ended up combining word count, the length of the text, the half way point and a third of the string. This allowed me to make sure the white section is going to be somewhere between the third way/half way mark.
I hope it helps someone. Be aware in my code -,& etc would be considered a word - it didn't cause me issues in my testing but I will update this if I find its not working as I hope.
这是类示例:
使用方法:
将返回 2 个元素的数组。
Here is class example:
The way to use it:
that will return 2 elements array.
只需更改行:
str_word_count()
可能不会将 : 或 / 计为单词。Just change the line:
str_word_count()
may not count : or / as word.