preg_replace,添加空格时忽略

发布于 2024-12-10 14:22:39 字数 791 浏览 0 评论 0原文

我有这段简单的代码可以将 *text* 转换为 text

这一切都很好,但现在我还希望能够使用 * 来制作列表,例如:

* item 1
* item 2
* item 3

这显然不适用于我当前的代码。有没有办法更改代码,以便忽略 * (旁边有一个空格)?

这是我当前的代码:

$content = preg_replace('#\*(.*?)\*#is', '<strong>$1</strong>', $content);

编辑:

抱歉,我的示例可能有点不清楚。

所以这是原始输入:

*test*

* test
* test
* test

   *test*

这应该格式化为:

<strong>test</strong?

* test
* test
* test

   <strong>test</strong>

所以基本上 *test* 应该显示为 test,除非有* 旁边的空格。 所以* test,仍会是* test

有点像basecamp中使用的格式

I have this simple piece of code to turn *text* into <strong>text</strong>.

This all works great, but now I also want to be able to use * for making lists, like:

* item 1
* item 2
* item 3

This will obviously not work with my current code. Is there a way to change the code so that * (with a space next to them) are ignored?

This is my current code:

$content = preg_replace('#\*(.*?)\*#is', '<strong>$1</strong>', $content);

EDIT:

Sorry, I might have been a bit unclear with my example.

So this is the original input:

*test*

* test
* test
* test

   *test*

This should be formatted as:

<strong>test</strong?

* test
* test
* test

   <strong>test</strong>

So bassicly *test* should show up as <strong>test</strong>, unless there is a space right next to the *.
So * test, will remain * test

It's a little like the formatting used in basecamp

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

恋你朝朝暮暮 2024-12-17 14:22:39

您可以使用 [^\s] 来匹配任何非空格字符(您也可以使用 \b 来获取单词边界,但您会遇到非空格字符的问题单词字符)。你的代码将是这样的:

$content = preg_replace('#\*([^\s\*]([^\*]*[^\s\*])?)\*#is', '<strong>$1</strong>', $content);

干杯,

You may use [^\s] to match any non-space character (you could also use\b to get a word boundary, but you would have issues with non-word characters). Your code would be like this:

$content = preg_replace('#\*([^\s\*]([^\*]*[^\s\*])?)\*#is', '<strong>$1</strong>', $content);

Cheers,

空袭的梦i 2024-12-17 14:22:39

抱歉,我可以确认您想要实现的目标吗?

您想将其更改

 * item 1 * item 2 * item 3

* <strong>item 1</strong> * <strong>item 2</strong> * <strong>item 3</strong>

我正确吗?

Sorry, can i confirm what you are trying to achieve?

You want to change this

 * item 1 * item 2 * item 3

into

* <strong>item 1</strong> * <strong>item 2</strong> * <strong>item 3</strong>

Am I correct?

土豪 2024-12-17 14:22:39

您需要字符类的逆,即 [^ ]。当我开始使用正则表达式时,我发现这非常有价值。

编辑:

我应该已将其放入您的示例中。

'#[^ ]\*(.*?)\*#is'

编辑:评论中澄清后的测试示例:

$in = "
*test*
 * test
  * test

       *test*
";
echo preg_replace('/\*([^ \*]+)\*/', '<strong>$1</strong>', $in);

编辑:进一步澄清。当替换匹配的字符时,例如两个 * 之间的任何字符,更容易说“星号,然后是非星号的任何字符,后跟结束星号”,而不是使用 .从字面上匹配您想要的简单英语描述中的“任何东西”一词。它也更有效,与回溯或其他东西有关(我不确定原因是什么)。由于您的列表赋予了尾随空格的重要性,因此我将其添加到了逆字符类中。

You want the inverse of a character class, i.e. [^ ]. I found this invaluable when starting out with regular expressions.

http://www.regular-expressions.info/reference.html

EDIT: I should have put it in your example.

'#[^ ]\*(.*?)\*#is'

EDIT: A tested example after clarification in the comments:

$in = "
*test*
 * test
  * test

       *test*
";
echo preg_replace('/\*([^ \*]+)\*/', '<strong>$1</strong>', $in);

EDIT: Further clarification. When replacing matched characters, like anything between two *'s, it's easier to say 'an asterisk, then anything that isn't an asterisk, followed by a closing asterisk' rather than using a . to match literally the word 'anything' in the plain-english description of what you want. It's also more efficient, having to do with back tracking or something (I'm not sure what the reason is). Since your lists give significance to the trailing spaces I added that to the inverse character class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文