Php 操作后如何恢复字符串格式
我需要提取每个单词的句子或段落,并将单词转换成另一种形式。例如,我需要将“面条”一词改为“面条”。我用这段代码将句子分解成单词,并使用Php 操作后如何恢复字符串格式,php,regex,Php,Regex,我需要提取每个单词的句子或段落,并将单词转换成另一种形式。例如,我需要将“面条”一词改为“面条”。我用这段代码将句子分解成单词,并使用infrade()函数进行组合 function display_sentence_with_answer($str="") { $arr_output = []; $str = preg_replace("#<p>(\s| |</?\s?br\s?/)*</?p>#","",$str); $words = p
infrade()
函数进行组合
function display_sentence_with_answer($str="")
{
$arr_output = [];
$str = preg_replace("#<p>(\s| |</?\s?br\s?/)*</?p>#","",$str);
$words = preg_replace('#<[^>]+>#', ' ', $str);
$arr_words = preg_split('/<[^>]+>(?:\s+<[^>]+>)*|\s+/u', trim($words));
foreach($arr_words as $word)
{
$arr_output[] = '##'.$word.'##';
}
$output_str = implode(" ",$arr_output);
return $output_str;
}
如何恢复我当前的输入格式?有人经历过这种需求吗
我的预期产出是:
##Nyatakan## ##pecahan## ##bagi## ##rajah## ##di## ##bawah.##
<br/>
##4/5##
<p>
p</p>
##################################巴瓦##
##4/5##
p
谢谢 您可以将此正则表达式与PCRE动词
(*SKIP)(*F)
一起使用,以跳过某些匹配项:
(?:<([^>]*)>.*?</\1>|<[^>]*/>)(*SKIP)(*F)|\b\w\S*
(?:]*)>.*?]*/>)(*跳过)(*F)|\b\w\S*
正则表达式分解:
(?: # start non capturing group
<([^>]*)>.*?</\1> # match a tag and closing tag <tag>...</tag>
| # OR
<[^>]*/> # match a tag like <tag/>
) # end non capturing group
(*SKIP)(*F) # skip this match
| # OR
\b\w\S* # match a word starting with a word character
(?:#启动非捕获组
]*)>.*? # 匹配标记和结束标记。。。
|#或
]*/>#匹配一个标签,如
)#结束非捕获组
跳过这场比赛
|#或
\b\w\S*#匹配以单词字符开头的单词
注意:HTML不是一种常规语言,它可能非常不可预测,不建议使用正则表达式解析HTML。您可以返回一个包含两个元素的数组:原始字符串和处理过的字符串。@user1915746没有真正的帮助,因为他仍然需要确定哪些字符串中包含
(?:<([^>]*)>.*?</\1>|<[^>]*/>)(*SKIP)(*F)|\b\w\S*
(?: # start non capturing group
<([^>]*)>.*?</\1> # match a tag and closing tag <tag>...</tag>
| # OR
<[^>]*/> # match a tag like <tag/>
) # end non capturing group
(*SKIP)(*F) # skip this match
| # OR
\b\w\S* # match a word starting with a word character