如何按指定格式显示PHP字符串?附示例原始字符串
没问题!我帮你把这段PHP字符串里的语录内容整理成规范的Markdown格式,还附上PHP代码示例,让你能直接实现这个转换:
整理后的Markdown格式语录
Joy is the serious business of heaven.
— Lewis, Clive Staples, 1964-01-01
We were not meant to be somebody--we were meant to know Somebody
— Piper, John Stephen, 2011-07-17
He who sings prays twice.
— Hipponensis, Aurelius Augustinus, 430-02-30
The task of the modern educator is not to cut down jungles but to irrigate deserts.
— Lewis, Clive Staples, 1943-09-23
There is not one blade of grass, there is no color in this world that is not intended to make us...
— [未完整记录]
PHP代码实现(自动转换原始字符串为Markdown)
<?php $raw = "'Joy is the serious business of heaven.'.LEWIS,CLIVE STAPLES.1964-01-01. |'We were not meant to be somebody--we were meant to know Somebody'.PIPER,JOHN STEPHEN.2011-07-17.|'He who sings prays twice.'.Hipponensis,Aurelius Augustinus.430-02-30. |'The task of the modern educator is not to cut down jungles but to irrigate deserts.'.LEWIS,CLIVE STAPLES.1943-09-23. |'There is not one blade of grass, there is no color in this world that is not intended to make us..."; // 分割每条语录 $quotes = explode('|', $raw); echo "# 精选语录集\n\n"; foreach ($quotes as $quote) { // 清理首尾空格 $quote = trim($quote); if (empty($quote)) continue; // 拆分引用内容、作者、日期 $quote = ltrim($quote, "'"); $firstDotPos = strpos($quote, "'."); if ($firstDotPos === false) { // 处理不完整的条目 echo "- \n > " . $quote . "\n > \n > *— [未完整记录]*\n\n"; continue; } $quoteText = substr($quote, 0, $firstDotPos); $rest = substr($quote, $firstDotPos + 2); // 分割作者和日期 $parts = explode('.', $rest); $author = trim($parts[0] ?? ''); $date = trim($parts[1] ?? ''); // 格式化作者名(大写转首字母大写,调整逗号格式) $author = ucwords(str_replace(',', ', ', strtolower($author))); echo "- \n > " . $quoteText . "\n > \n > *— " . $author . ($date ? ", " . $date : "") . "*\n\n"; } ?>
内容的提问来源于stack exchange,提问作者Roy Murwa
相关产品推荐
相关产品推荐

