小编pag*_*gex的帖子

需要正则表达式在长单词中添加空格但忽略html标签和属性

我需要在用户提供的位置(例如我们会说25)中在产品描述中的单词中添加空格以允许正确的包装.我知道可以使用css技巧,但这不是我喜欢的东西.

到目前为止,我可以使用这种语法来做到这一点,但我遇到的问题是它的分裂内容不应该像html标记属性中的URL一样分裂.

    $string = 'longwordlongwordlongword <a href="http://www.somelongdomainname.com/and-a-long-sub-directoty_name" class="some_long_class_name_here">someanchortext and title here</a>';

    $spacer = 20;

    $newtext = preg_replace('/([^\s]{' . $spacer . '})(?=[^\s])/m', '$1 ', $newtext);
Run Code Online (Sandbox Code Playgroud)

结果是......

    longwordlongwordlong word <a href="http://www.som elongdomainname.com/ and-a-long-sub-direc toty_name" class="some_long_cla ss_name_here">somean chortext and title here</a>
Run Code Online (Sandbox Code Playgroud)

所以基本上我需要以某种方式告诉正则表达式除了html标签和属性之外的一切.

任何帮助都是极好的.谢谢.

php regex split

4
推荐指数
1
解决办法
571
查看次数

标签 统计

php ×1

regex ×1

split ×1