With jQuery or PHP, I would like to modify the dom structure of the the_content function in WordPress. In some posts I use the h3 element, and I would like to add a wrapper that contains the content until the next h3.
So I would like to convert this:
<h3>Title</h3>
<p>This is just regular text</p>
<h3>Next title</h3>
Into this:
<div class='wrapper'>
<h3>Title</h3>
<p>This is just regular text</p>
</div>
<div class='wrapper'>
<h3>Next title</h3>
</div>
Thanks!
assuming that the content only consists of <h3>s and <p>s,and they are welled formated like:
<div id="content">
<h3>title</h3>
<p>..........</p>
<p>..........</p>
<h3>another title</h3>
<p>.........</p>
<h3>yet another title</h3>
<p>..........</p>
</div>
then you may try this in jQuery.
//get the main post content
$content=$("#content");
$h3s=$content.find('h3');
$h3s.each(function(index){
if(index==0)$(this).before('<div class="wrapper">');
else $(this).before('</div><div class="wrapper">');
});
//remeber to close the last one
$content.append('</div>');
Related
I need access a nested element with repeated class, like that:
<div class="container">
<div class="first"></div>
<div class="first"></div>
<div class="first">
<div class="second"></div>
<div class="second">
<p>I need that text</p>
</div>
</div>
</div>
So i try something like that:
$localizacao_x = $xpath_det_page->query('//div[#class="container"]/div[#class="first"][3]/div[#class="second"][2]/p');
$localizacao = $localizacao_x->item(0)->nodeValue;
echo "[Localizacao] : [".$localizacao."]"."<br/>";
But result in non object, any tip?
Your XPath seems to be correct. I tested
//div[#class="container"]/div[#class="first"][3]/div[#class="second"][2]/p
which result is
I need that text
This is my scenario: In a custom CMS developed in PHP, I need to parse HTML string searching some custom tags to replace them with some HTML code. Here is an example for clarifying:
<h2>Some Title</h2>
<p>Some text</p>
[[prod_id=123]] [[prod_id=165]] // custom tag
<p>More text</p>
I need to find the custom tags and replace them with the template of the item, as a result:
<h2>Some Title</h2>
<p>Some text</p>
<!--Start Product123-->
<p>Title Product 123</p>
<!--End Product123-->
<!--Start Product165-->
<p>Title Product 165</p>
<!--End Product165-->
<p>More text</p>
This would be very helpful, but I need to do something else, I need to detect blocks of tags and add some code before - after the tags, but only once per block of tags. In this example, the final code needed would be something like:
<h2>Some Title</h2>
<p>Some text</p>
<div><!-- Here the start of the block -->
<!--Start Product123-->
<p>Title Product 123</p>
<!--End Product123-->
<!--Start Product165-->
<p>Title Product 165</p>
<!--End Product165-->
</div><!-- Here the end of the block -->
<p>More text</p>
The perfect solution for me would be a function with the original HTML code as argument, and returning the final html code. Any help is appreciated.
I will advise you to not use Regex along with HTML, this can result in a lot of problems. Instead do something like where you store the text/content of articles and then only process that.
But for the sake of completeness, you can use something like this:
$html = preg_replace_callback("/\[\[prod_id=(\d+)\]\]/",
function($matches)
{
$prod_id = $matches[1];
return '<p>Title Product ' . $prod_id . '</p>';
},
$html); // where $html is the html you want to process
If you don't "have" the HTML, then you can use ob_start() and ob_get_clean().
ob_start();
?>
<h2>Some Title</h2>
<p>Some text</p>
[[prod_id=123]] [[prod_id=165]] // custom tag
<p>More text</p>
<?php
$html = ob_get_clean();
// do the regex_replace_callback here
I haven't tested this, just did it on top of my head. So there might be some typos!
This is my HTML example:
<div id="Texte">
<div class="pagination">
...
</div>
<p>...</p>
<p>....</p>
<p class="Foot">...</p>
</div>
I want to use Xpath to get all content of my <div id="Texte"> without the <p class="foot">.
I use this, but it's not ok, I have the class='Foot' in my result :
$crawler->filterXPath("//*[#id='Texte' and not(#class='Foot')]")->html();
Almost.
// correct
$crawler->filterXPath("//*[#id='Texte']/*[not(#class='Foot')]")->html();
// yours, for comparison
$crawler->filterXPath("//*[#id='Texte' and not(#class='Foot')]")->html();
I have a HTML file that I'm trying to parse. It has a bunch of DIVs like this:
<div class="doc-overview">
<h2>Description</h2>
<div id="doc-description-container" class="" style="max-height: 605px;">
<div class="doc-description toggle-overflow-contents" data-collapsed-height="200">
<div id="doc-original-text">
Content of the div without paragraph tags.
<p>Content from the first paragraph </p>
<p>Content from the second paragraph</p>
<p>Content from the third paragraph</p>
</div>
</div>
<div class="doc-description-overflow"></div>
</div>
I tried this:
foreach($html->find('div[id=doc-original-text]') as $div) {
echo $div->innertext;
}
You notice that I directly find the doc-original-text but I also tried to parse from outer divs to inner divs.
Try This,
foreach($html->find('div#doc-original-text') as $div) {
echo $div->innertext;
}
so i need to take the whole div with class "1" but it stops at the div class "1.1" ending so i want to get from this:
<head>
</head>
<body>
<div class="1">
<p>blah blah blah</p>
<div class="1.1">
trolololol
</div>
<div class="1.2">
trolo2lolo
</div>
</div>
</body>
only this:
<div class="1">
<p>blah blah blah</p>
<div class="1.1">
trolololol
</div>
<div class="1.2">
trolo2lolo
</div>
</div>
but for now i get only:
<div class="1">
<p>blah blah blah</p>
<div class="1.1">
trolololol
</div>
Regexp are not that intelligent to count how many tags you have opened and need to be closed before stopping the match. It stops at the first occurence of </div>. Try to use a real html parser if you want to access tags as real tags and not strings.
Regular expressions should not be used to parse documents like XML, HTML, "BBCode", JSON...
You should look for a real DOM parser, for example PHP's DOM extension