php - php将word设置为粗体的问题

标签 php regex pcre

好的,我有一个输出一些文本的字符串。

~ Terraces
The apartment has two large terraces. Main terrace (30m2) is accessible from the living room, is fitted with a large wooden table with 6 armchairs and large comfortable wooden sunbeds. The views from here are amazing, you can admire incredible sea views but also you have a fascinating view of La Concha, the striking mountain that sits behind Marbella. Second terrace (14m2) is accessible from the kitchen.

我唯一想做的就是制作像这样的模式 ~ string 转换为 string。更具体地说,是为了摆脱 ~、空格和将文本设为粗体。

我创建了这个函数:

function bold($var) {
$var = preg_replace('/~\s(.*?)/', '<strong>$1</strong>', $var);
return $var;
}

该函数删除了〜但不会使文本变为粗体。为什么?更奇怪的是,如果我在强标记之间插入一些内容,我会得到粗体文本,然后是我想要非粗体的文本,即使它们'位于相同的标签中。

简单说明一下...$var 是整个字符串,就像上面示例中的那样。

最佳答案

我相信你需要这个:

$var = preg_replace('/~\s*(\S+)/', '<strong>$1</strong>', $var);

关于php - php将word设置为粗体的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20172249/

相关文章:

PHP session_write_close() 不断发送 set-cookie header

php - 在 Laravel 应用程序中访问核心 PHP 应用程序 session 时遇到的问题

regex - 在 Emacs 正则表达式中匹配双花括号?

php - 用于验证 ip-list 中的 ip-range 的正则表达式

php - Magento 为媒体使用第二个数据库 - SQLSTATE [42S02] : Base table or view not found

java 正则表达式 帮助

javascript - 如何编写正则表达式来匹配我的字符串

linux - 是否可以在 grep 正则表达式中重复匹配?

r - 量词可以用于 R 中的正则表达式替换吗?

php - Laravel 与 SQL Server 2008 抛出 "Conversion of a varchar data type to a datetime data type resulted in an out-of-range value"