[英]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']);
\\b
matches inital word boundary of first word \\b
匹配第一个单词的初始单词边界 [\\w'-]+
one or more word characters , single quote and hyphen in a character class [\\w'-]+
字符类中的一个或多个单词字符 ,单引号和连字符 \\W+
one or more non-word characters \\W+
一个或多个非单词字符 \\K
resets beginning of the reported match \\K
重置报告的比赛开始 .*\\S
anything until the last non white space character .*\\S
直到最后一个非空格字符的所有内容 s
- flag for making the dot match newlines s
使点与换行符匹配的标志 u
flag for unicode regex to adjust \\b
, \\w
, \\W
u
标志以调整\\b
, \\w
, \\W
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.