How replace all spaces inside HTML elements with   using preg_replace?

三世轮回 提交于 2019-12-17 20:24:25

问题


I need replace spaces with   inside HTML elements. Example:

<table atrr="zxzx"><tr>
<td>adfa a   adfadfaf></td><td><br /> dfa  dfa</td>
</tr></table>

should become

<table atrr="zxzx"><tr>
<td>adfa&nbsp;a&nbsp;&nbsp;&nbsp;adfadfaf></td><td><br />&nbsp;dfa&nbsp;&nbsp;dfa</td>
</tr></table>

回答1:


use regex to catch data between tags

(?:<\/?\w+)(?:\s+\w+(?:\s*=\s*(?:\".*?\"|'.*?'|[^'\">\s]+)?)+\s*|\s*)\/?>([^<]*)?

then replace ' ' with '&nbsp;'

also to catch before and after html :

^([^<>]*)<?

>([^<>]*)$

Edit: here you go....

<?php
$data="dasdad asd a  <table atrr=\"zxzx\"><tr><td>adfa a   adfadfaf></td><td><br /> dfa  dfa</td></tr></table>  asdasd s ";
$exp="/((?:<\\/?\\w+)(?:\\s+\\w+(?:\\s*=\\s*(?:\\\".*?\\\"|'.*?'|[^'\\\">\\s]+)?)+\\s*|\\s*)\\/?>)([^<]*)?/";

$ex1="/^([^<>]*)(<?)/i";
$ex2="/(>)([^<>]*)$/i";

$data = preg_replace_callback($exp, function ($matches) {
    return $matches[1] . str_replace(" ", "&nbsp;", $matches[2]);
}, $data);
$data = preg_replace_callback($ex1, function ($matches) {
    return str_replace(" ", "&nbsp;", $matches[1]) . $matches[2];
}, $data);
$data = preg_replace_callback($ex2, function ($matches) {
    return $matches[1] . str_replace(" ", "&nbsp;", $matches[2]);
}, $data);

echo $data;
?>

it works... slightly modified but it would work without modifications (but i dont think youd understand the code ;) )




回答2:


If you're working with php, you can do

$content = str_replace(' ', '&nbsp;', $content);



回答3:


Since tokenizing HTML with regular expressions can be quite complicated (especially when allowing SGML quirks), you should use an HTML DOM parser like the one of PHP’s DOM library. Then you can query the DOM, get all text nodes and apply your replacement function on it:

$doc = new DOMDocument();
$doc->loadHTML($str);
$body = $doc->getElementsByTagName('body')->item(0);
mapOntoTextNodes($body, function(DOMText $node) { $node->nodeValue = str_replace(' ', '&nbsp;', $node->nodeValue); });

The mapOntoTextNodes function is a custom function I had defined in How to replace text URLs and exclude URLs in HTML tags?



来源:https://stackoverflow.com/questions/5210287/how-replace-all-spaces-inside-html-elements-with-nbsp-using-preg-replace

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!