【发布时间】:2011-07-10 12:20:34
【问题描述】:
我需要在用户提供的位置(例如我们会说 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);
结果是这样的……
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>
我需要以某种方式告诉正则表达式拆分除 HTML 标记和属性之外的所有内容。
【问题讨论】:
-
只需要替换第一个
<a>吗?字符串中是否只有一个标签,即<a>? -
不要使用正则表达式:stackoverflow.com/questions/1732348/…
-
您应该以编程方式执行此操作。
Regex无法做到这一点。