Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/249.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将样式应用于DIV内的纯文本,而不影响DIV的其他子级_Php_Css_Css Selectors - Fatal编程技术网

Php 将样式应用于DIV内的纯文本,而不影响DIV的其他子级

Php 将样式应用于DIV内的纯文本,而不影响DIV的其他子级,php,css,css-selectors,Php,Css,Css Selectors,给定一个具有此一般结构class=post的DIV,该DIV来自一个提取的留言板post 我需要将CSS样式应用于post DIV的纯文本,而不涉及其其他子元素Ps、表格、其他DIV等。在收到post DIV之前,我无法更改其构造方式;我可以在收到它之后和输出它之前尽可能多地修改它。我正在使用PHP输出HTML。有一个链接的CSS样式表,如果需要可以采用其他样式。post DIV中可能有任意数量的纯文本块需要设置样式,也可能有任意数量的子元素需要单独设置 我一直在使用PHP的字符串处理函数str

给定一个具有此一般结构class=post的DIV,该DIV来自一个提取的留言板post

我需要将CSS样式应用于post DIV的纯文本,而不涉及其其他子元素Ps、表格、其他DIV等。在收到post DIV之前,我无法更改其构造方式;我可以在收到它之后和输出它之前尽可能多地修改它。我正在使用PHP输出HTML。有一个链接的CSS样式表,如果需要可以采用其他样式。post DIV中可能有任意数量的纯文本块需要设置样式,也可能有任意数量的子元素需要单独设置

我一直在使用PHP的字符串处理函数stripos、stripos、substr。下面的代码成功地将纯文本包装在第一个子元素之前,我不确定为什么这些项。。。在上一次之后我打赌有很多司法管辖区。。。在P标签中。问题是访问子元素之间的纯文本,它们已经必须

在切换到字符串处理之前,我花了几个小时的时间在PHP的DOMDocument/DomeElement类上敲打,如果可能的话,我很乐意留在这里。我真正需要的就是在DIV的顶层用一些简单的编程方式访问纯文本。如果存在这样的事情


提前感谢。

如果您的目标浏览器只支持足以支持较新选择器的CSS3,那么您可以使用:not to exclude all child

像这样的方法应该会奏效:

div.post *:not(div)  
div.post *:not(table)  
:not选择器没有很好的文档记录,它更多的是人们尝试它时的反复尝试。请看这里:

但除此之外,我认为最好的选择是以这样一种方式设置CSS,即每个子元素及其子元素的样式都需要显式定义。这将确保应用于父div的样式仅影响子div之外的文本内容

例如


如果您只能针对支持CSS3的浏览器,这些浏览器足以支持较新的选择器,那么您可以使用:not排除所有子项

像这样的方法应该会奏效:

div.post *:not(div)  
div.post *:not(table)  
:not选择器没有很好的文档记录,它更多的是人们尝试它时的反复尝试。请看这里:

但除此之外,我认为最好的选择是以这样一种方式设置CSS,即每个子元素及其子元素的样式都需要显式定义。这将确保应用于父div的样式仅影响子div之外的文本内容

例如


我会使用两个加法索引,startOfValidArea和endOf。。。 该区域是文本和索引所在的位置,您应该在其中放置和

标记或任何标记 然后移动这些标记,如:
开始。。设置为根的结尾并找到的第一个位置,我将使用两个加法索引,startOfValidArea和endOf。。。 该区域是文本和索引所在的位置,您应该在其中放置和

标记或任何标记 然后移动这些标记,如: 开始。。设置为根的结尾,并查找根的第一个位置 至少在safari工作。 你为什么用PHP来做这件事

至少在safari工作。
你为什么要在PHP中使用它?

老实说,我看不出否定选择器在这里有什么帮助。老实说,我看不出否定选择器在这里有什么帮助。我在考虑某种refcount方案,每个方案减去一个;当refcount为零时发现的任何文本都被包装在P标记中;refcount为零时发现的任何文本都会被包装在P tags.PHP中,因为整个站点都是PHP,包括从vBulletin留言板的MySQL数据库后端获取帖子的代码。因此,我尝试使用PHP将CSS类添加到未设置样式的文本中。如果我不必添加类,但可以使用您显示的选择器,那就更好了。我将尝试一下try.PHP,因为整个站点都是PHP,包括从vBulletin留言板的MySQL数据库后端获取帖子的代码。因此,我尝试使用PHP将CSS类添加到未设置样式的文本中。如果我不必添加类,但可以使用您显示的选择器,那就更好了。我要试一试。
div.post *:not(div)  
div.post *:not(table)  
div.post { font-family:tahoma }
div.post div { font-famile:arial }
div.post table { font-family:verdana }
$textStart = stripos($postMessage, '>')+1;            // startig index for the text to highlight
$textEnd = stripos($postMessage, '<', $textStartPos);     // ending index for the text to highlight

while($textStart != false && $textEnd != false){
    // while there is still any text to highlight

    // insert the highlighting code
    $postMessage = substr ($postMessage, 0, $textStart) . '<p class=\"postMessage\">' . substr ($postMessage, $textStart, $textEnd) . '</p>' . substr ($postMessage, $textEnd, strlen($postMessage);

    // get the tag
    $endOfTag = (stripos($postMessage, ' ', $textEnd) < stripos($postMessage, '>', $textEnd)) ? stripos($postMessage, ' ', $textEnd) : stripos($postMessage, '>', $textEnd);
    $tag = substr ($postMessage, $textEnd, $endOfTag); // getting the tag here

    if($tag == '<br' || $tag == '<img'){
        // do smthing with not-paired tag, like check if the tag is ending with the '/>' string and then ignore it and get the next tag
    }

    $closingTag = '</'.substr($tag,1,strlen($tag));           // creating the closing tag like </div or </p    
    $nextSameTag = stripos($postMessage, $tag, $endTag);
    $nextClosingTag = stripos($postMessage, $closingTag, $nextSameTag);

    // loop through the same inner tags like <div>text<div>text</div>text</div>
    while($nextSameTag < $nextClosingTag && $textStart != false && $textEnd != false){  
        $nextSameTag = stripos($postMessage, $tag, $nextClosingTag);
        $nextClosingTag = stripos($postMessage, $closingTag, $nextSameTag);
    }
    $textStart = stripos($postMessage, '>', $nextClosingTag)+1;
    $textEnd = stripos($postMessage, '<', $nextClosingTag);
}
body {color:blue}    
div.post {color:red}
div.post * {color:blue}