DOMDocument将属性添加到根标记(DOMDocument add attribute to root tag)
我想创建一些函数,为给定的html的root标签添加一些属性。
我这样做:
$dom = new \DOMDocument(); $dom->loadHTML($content); $root = $dom->documentElement; $root->setAttribute("data-custom","true");
而对于
$content='<h1 class="no-margin">Lorem</h1>'
它返回:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"> <html data-custom="true"><body><h1 class="no-margin">Do more tomorrow. For less.</h1></body></html>
虽然应该只是:
<h1 data-custom="true" class="no-margin">Lorem</h1>
如何让DOMDocument不创建doctype,html,body标签,而只是对给定的html进行操作以及如何选择给定html的根节点
PS。 我永远不会使用正则表达式来管理HTML。
I want to make function that adds some attributes to root tag of given html.
I'm doing this:
$dom = new \DOMDocument(); $dom->loadHTML($content); $root = $dom->documentElement; $root->setAttribute("data-custom","true");
And for
$content='<h1 class="no-margin">Lorem</h1>'
It returns:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"> <html data-custom="true"><body><h1 class="no-margin">Do more tomorrow. For less.</h1></body></html>
While should be just:
<h1 data-custom="true" class="no-margin">Lorem</h1>
How to make DOMDocument not create doctype, html, body tags, but just operate on given html and how to select the root node(s) of given html
Ps. I will never use regex to manage html.
原文:https://stackoverflow.com/questions/18758101
最满意答案
它是拉丁语1减去代码点U + 0000,U + 0001和U + 0002。 这包括可以在美国键盘上找到的常用内容,大量的控制字符(低于U + 0020以及U + 007F和U + 009F之间)和一些其他拉丁字符,可以用来写大部分西欧语言。
It's Latin 1 minus the code points U+0000, U+0001 and U+0002. This includes the usual stuff that can be found on the US keyboard, plenty of control characters (below U+0020 and between U+007F and U+009F) and a few other Latin characters that can be used to write the majority of Western European languages.
相关问答
更多-
使用raw_unicode_escape : text = 'Cze\u00c5\u009b\u00c4\u0087' text_bytes = text.encode('raw_unicode_escape') print(text_bytes.decode('utf8')) # outputs Cześć Use raw_unicode_escape: text = 'Cze\u00c5\u009b\u00c4\u0087' text_bytes = text.encode('raw_unicode_ ...
-
>>> u"ÿ".encode('raw-unicode-escape') '\xff' r"\u%04X" % ord(u"ÿ") This did the trick for me. It returns a string object ('\\u00FF') which I can use to make a string compare. It fails for unicode characters above U+FFFF but this is not necessary in my ca ...
-
尝试这个: $str = preg_replace_callback('/\\\\u([0-9a-fA-F]{4})/', function ($match) { return mb_convert_encoding(pack('H*', $match[1]), 'UTF-8', 'UCS-2BE'); }, $str); 如果是基于UTF-16的C / C ++ / Java / Json样式: $str = preg_replace_callback('/\\\\u([0-9a-fA-F]{4 ...
-
考虑: >>> print '\u2022' \u2022 >>> print len('\u2022') 6 >>> import unicodedata >>> map(unicodedata.name, '\u2022'.decode('ascii')) ['REVERSE SOLIDUS', 'LATIN SMALL LETTER U', 'DIGIT TWO', 'DIGIT ZERO', 'DIGIT TWO', 'DIGIT TWO'] >>> VS: >>> print u'\u2022 ...
-
Eclipse默认使用平台默认编码(在Windows中为cp1252)在保存基于文本的文件期间以及在写入标准输出流(由System.out )期间解码字符。 您需要明确地将其设置为UTF-8才能实现统治世界。 请注意,这样您也不需要再使用那些\uXXXX Unicode转义来表示基于文本的源文件中的这些字符。 引发这些问号是因为输出流使用的字符集不支持输入流中指定的字符。 也可以看看: Unicode - 如何使角色正确? Eclipse uses by default platform default e ...
-
这个正则表达式中这些Unicode字符(代码点)的含义是什么?(What do these Unicode characters (codepoints) mean in this regex?)[2022-06-16]
很奇怪......根据Windows上的人物地图,我会说“À到ÿ” 这些是A,C,E,I,D,N,O,U,Y,德国夏普的一些变化(重音,cedillas)...... weird... according to the character map on Windows I'd say "À to ÿ" Those are some variations (accents, cedillas) on A, C, E, I, D, N, O, U, Y, the german Sharp s, ... -
哪个单字节字符集与第一个0x100 unicode字符匹配?(Which single-byte charset matches first 0x100 unicode characters?)[2022-07-03]
ISO-8859-1按照设计匹配最接近的第一个Unicode代码点。 ISO-8859-1 matches the first Unicode code points the closest, by design. -
为了处理Unicode字符,Vim必须使用能够表示这些字符的'encoding' 。 如果值为latin1 ,则无法对所提到的字符进行编码(此8位编码仅包括ASCII和几个西欧字符,请参见此处 )。 所以,你需要 :set encoding=utf-8 这样,任何新创建的文件都将使用该编码,您应该能够插入Unicode字符并将其写入(也可以使用其他Unicode文件编码,例如:w ++enc=ucs-2le ;但如果您尝试保留为:w ++enc=latin1 ,你会得到一个CONVERSION ERROR ...
-
好的,所以你有一个任意的字符串(事实上它包含不可打印的字符是无关紧要的),你想用UTF-8将它转换成一个字节数组。 这很容易 :) byte[] bytes = Encoding.UTF8.GetBytes(text); 或者要写StreamWriter ,通常将其包装在StreamWriter : // Note that due to the using statement, this will close the stream at the end // of the block using (va ...
-
它是拉丁语1减去代码点U + 0000,U + 0001和U + 0002。 这包括可以在美国键盘上找到的常用内容,大量的控制字符(低于U + 0020以及U + 007F和U + 009F之间)和一些其他拉丁字符,可以用来写大部分西欧语言。 It's Latin 1 minus the code points U+0000, U+0001 and U+0002. This includes the usual stuff that can be found on the US keyboard, plen ...