开发者

Find particular lines and wrap them with <b>

I want to wrap particular lines in a text ma开发者_StackOverflow社区ss with <b></b>

  • First line in the text
  • All lines with a previous empty line (eg two newlines before)

I'm using preg_replace with php, but I'm really shitty with regex. Good tutorials are appreciated.


Try

echo preg_replace('/(?<=\\A|^$\n)(^.+$)/m', '<b>$1</b>', $text);

for example, http://www.ideone.com/1pTwD.


In HTML (I'm assuming that's the context) there is no such thing as "lines" in regards to source code, since it's not really possible reliable to determinate how a text wraps (unless you do all the wrapping yourself with <br> or <pre>).

However there is the CSS pseudo-element :first-line that will let you format the first line of an element (for example a paragraph):

p:first-line {
  font-weight: bold;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜