问题描述
如果在之前或之后没有文字出现,我该如何删除< br />
?
例如,
< p>< br /> hello< / p>
< p> hello< br />< / p>
它们应该像这样被重写,
< p为H.你好< / p为H.
< p> hello< / p>
我应该使用DOMxpath还是正则表达式会更好?
(注意:我有一个关于删除< p>< br />< / p>
,然后我遇到了这个问题!)
编辑:
如果我在输入中有这个,
$ content ='< p>< br /> hello<> hello< br>< / p>';
那么它应该是
< p> hello<> hello< / p>'
// p [节点()[1] [self :: br]] / br [1] | // p [node()[last()] [self :: br]] / br [last()]
或(可能)更快:
// p [br] / node()[self :: br and(position()= 1 or position()= last())]
当p的第一个(或最后一个)节点是br时,只需获取br。
这会选择br,例如:
< p> <峰; br />你好< / p为H.
< p> hello< br />< / p>
和第一个和最后一个br一样:
< p为H.;<峰; br />你好<峰; br />你好<峰; br />< / p为H.
不在中间br:
< p为H.你好<峰; br />你好< / p为H.
PS:最终得到第一个br这样的< br />< br />
:
<$ c
[/ b] [下一个:: node()[1] [self :: br]
How can I remove <br/>
if no text comes before or after it?
For instance,
<p><br/>hello</p>
<p>hello<br/></p>
they should be rewritten like this,
<p>hello</p>
<p>hello</p>
Should I use DOMxpath or regex would be better?
(Note: I have a post about removing <p><br/></p>
with DOMxpath earlier, and then I came across this issue!)
EDIT:
If I have this in the input,
$content = '<p><br/>hello<br/>hello<br/></p>';
then it should be
<p>hello<br/>hello</p>'
To select the mentioned br you can use:
"//p[node()[1][self::br]]/br[1] | //p[node()[last()][self::br]]/br[last()]"
or, (maybe) faster:
"//p[br]/node()[self::br and (position()=1 or position()=last())]"
Just getting the br when the first (or last) node of p is br.
This will select br such as:
<p><br/>hello</p>
<p>hello<br/></p>
and first and last br like in:
<p><br/>hello<br/>hello<br/></p>
not middle br like in:
<p>hello<br/>hello</p>
PS: to get eventually the first br in a pair like this <br/><br/>
:
"//br[following::node()[1][self::br]]"
这篇关于我如何删除< br />如果之前或之后没有文字出现? DOMxpath或正则表达式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!