【发布时间】:2020-08-11 16:42:42
【问题描述】:
我正在构建一个实验性的 PHP 应用程序来处理 西里尔 UTF-8 字符的诗歌。我想实现以下目标:
- 计算每个字符的出现次数以及“所有辅音”等类别的总计数。它可能包含特殊字符和标点符号,只要我可以在输出中隐藏其中一些。我使用 UTF-8,所以我只能使用多字节函数。不可能使用 count_chars() :(
- 保留换行符和大写。我保留了具有不同格式的原始文本的多个副本。它们可能看起来多余,但我想尽可能多地保留信息。
- 根据条件更改某些字符的 HTML 格式,例如给元音和辅音不同的背景颜色,或突出显示所选字符的每个出现。据我了解,首先我需要将我的字符串分成几行(以保留中断),然后将它们中的每一个转换为一个包含 1 个字符的块的数组。对于输出,我将 join() 行返回。不幸的是,我找不到任何关于如何将 HTML 应用于数组值来解决像我这样的问题的想法。
我尝试了什么
除了不知道该怎么做之外,我还遇到了一些小问题。这是我现在做的一步一步。
我通过post方法收集一首诗。英文诗仅用于说明目的。
文本示例:
We shall not cease from exploration
And the end of all our exploring
Will be to arrive where we started
And know the place for the first time.
我对步骤进行了编号,希望使评论更容易。
1.获取带标签和不带标签的值
这是通过 textarea 提交后在htmlentities() 中的样子:
$string = "We shall not cease from exploration<br /> And the end of all our exploring<br /> Will be to arrive where we started<br /> And know the place for the first time."
我如何输出换行符:
$poem = nl2br($string);
这是没有标签的副本:
$droptags = strip_tags($poem);
2。计数字符
这是我在count_chars() 的初步尝试,缺少计数循环:
$poem2array = preg_split('//u', $droptags, null, PREG_SPLIT_NO_EMPTY);
$unique_characters = array_unique($poem2array);
输出如下:
(
[0] => W
[1] => e
[2] =>
...
)
3.将行拆分为数组
分割成行:
$lines = preg_split('<br />', $showtags);
我的问题是数组看起来像这样:
(
[0] => We shall not cease from exploration<
[1] => >
And the end of all our exploring<
[2] => >
Will be to arrive where we started<
[3] => >
And know the place for the first time.
)
我尝试将文本拆分为嵌套数组。我知道它坏了,因为我只能得到最后一行。
foreach($lines as $line) {
$line = preg_split('//u', $line, null, PREG_SPLIT_NO_EMPTY);
}
4. HTML 样式
至于数组的 HTML 样式,我不知道。我的参考数组看起来像这样:
$vowels = array("a", "e", "i");
$consonants = array("b", "c", "d");
$fontcolor = array("vowels" => "blue",
"consonants" => "orange");
【问题讨论】:
-
$lines = preg_split ('/
]*>/i', $string);试试这个没有 br 标签的数组。 -
如果你把这个问题分成多个帖子,我会更容易。我不确定其中哪一部分工作不正常,我应该解决哪一部分。
-
“这是它在 htmlentities() 中的样子”是什么意思?
标签: php html arrays string multibyte