简体   繁体   English

在第一个单词后的所有文本周围包装HTML标签

[英]Wrap html tags around all text after the first word

How can I rewrite this code such that it wraps html tags around the last words after the first word; 我该如何重写此代码,使它在第一个单词之后的最后一个单词周围包裹html标签;

preg_replace('/(?<=\>)\b\w*\b|^\w*\b/', '<span class="color_red">$0</span>', $atts['title']);

At the moment this regular expression will wrap tags around the first word in a string. 此刻,此正则表达式将在字符串的第一个单词周围包装标签。 I have tried this but it does not work; 我已经尝试过了,但是没有用;

preg_replace('/(?<=\>)\b\w*\b|^\w*\b/', '<span class="color_red">$1</span>', $atts['title']);

Here are some examples; 这里有些例子;

Efficiency <span class="color_red">solutions</span>
Renewable <span class="color_red">Energy Solutions</span>
Accidentally <span class="color_red">left the caps lock on and typed something</span>

Or please provide another solution that works. 或者,请提供其他可行的解决方案。

To match everything after the first word until the last non white space 匹配第一个单词之后到最后一个非空格的所有内容

preg_replace('/\b[\w\'-]+\W+\K.*\S/s', '<span class="color_red">$0</span>', $atts['title']);

See this demo at regex101 在regex101上观看此演示

You can do it with this way also.. 您也可以用这种方法来做。

$data = "Write your description here";
list($first,$last) = explode(" ", $data,2);
echo $newstring = $first." <span>".$last."</span>";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM