I have this code and i want first paragraph as output I tried to filter with paragraph but I am getting second paragraph
I am only interested in first paragraph text.
<div class="bq_fq_lrg" style="margin:0px">
<p>this text i want.</p>
<p class="bq_fq_a">
this text i dont want.
</p>
</div>
I tried this but it is giving second paragraph
foreach($html->find('div.bq_fq_lrg p[0]') as $e)
The $html variable is an instance of SimpleHtmlDom
I am getting the content of the paragraph like this:
$op1 = $e->innertext . '<br>';
You can use the ! in attributes to get that particular value. Consider this example:
include 'simple_html_dom.php';
$html_string = '<div class="bq_fq_lrg" style="margin:0px">
<p>this text i want.</p>
<p class="bq_fq_a">
this text i dont want.
</p>
</div>';
$html = str_get_html($html_string);
foreach($html->find('div.bq_fq_lrg p[!class]') as $value) {
echo $value->innertext; // this text i want.
}
Related
In PHP: Simple HTML DOM, How do I select all <strong> tag that are inside div with class abc, which are inside div with class 123:
<div class="123">
<div class="abc">
<strong>Text</strong>
</div>
</div>
You need to use a selector like div.123 div.abc strong and get the first element of the result. Here is a working example:
<?php
require 'simple_html_dom.php';
$html =<<<html
<div class="123">
<div class="abc">
<strong>Text</strong>
</div>
</div>
html;
$dom = str_get_html($html);
$el = $dom->find('div.123 div.abc strong', 0);
print $el;
print "\n";
print $el->innertext;
Result:
<strong>Text</strong>
Text
You can refer to the manual for a better understanding of how selectors work.
Neither of these work:
$html = file_get_html("https://www.example.com/page/");
print($html->find('[data-reactid=10]', 0)->plaintext);
print($html->find('[data-reactid=11]', 0)->plaintext);
where the html looks like this:
<div class="stuff" data-reactid="10">
<span data-reactid="11">Value I want</span>
</div>
what am I doing wrong?
FYI. this does work:
print($html->find('[data-reactid=5]', 0)->plaintext);`
where:
<div class"stuff" data-reactid="5">
<!-- react-text: 6 -->
Value I want
<!-- /react-text: -->
</div>
So how do I get the value with the span?
I can get the value with the div.
This works.
$html_str = '
<div class="stuff" data-reactid="10">
<span data-reactid="11">Value I want</span>
</div>
';
// Create a DOM object
$html = new simple_html_dom();
// Load HTML from a string
$html->load($html_str);
// Get the value
echo $html->find('div[data-reactid=10]', 0)->find('span', 0)->{'data-reactid'};
I change a variable to $result and then do echo $result, the result gets displayed at the very first line of the site, is this display position somehow changeable? Like, printing the output of echo $result into a little box at the bottom
if (isset($_POST['test']))
{
$result = "itwerks";
echo $result;
}
HTML Part:
<form method="post">
<button name="test">-> Test! <-</button>
</form>
As noted in the comments...
Move your PHP down so that it's not at the top of the page.
You can display PHP anywhere on the page.
<?php $var = 'This is some text, it is in a PHP variable'; ?>
<body>
<div>
<p>
This is some text, not in PHP
</p>
<p>
<?php echo $var //display some PHP here?>
</p>
</div>
</body>
You can do something like this:
$result = '<div style="position:absolute; bottom:2px;">itwerks</div>';
You have to create a HTML-Tag on the body of your document. In the tag you echo the text, e.g. $result. With CSS you can display the HTML-Tag as a box at the bottom.
I'm using PHP simple HTML DOM class to parse html.
I want to select div with id="content" inner text, but when I call $selecrot->plaintext, it also return sub div text
Sample HTML
<div id="content">
Hello World.
<div id="sub-content1">
Text I don't want to select.
</div>
<div id="sub-content2">
Text I don't want to select
</div>
</div>
Sample code
//suppose $html contains above html
$selector = $html->find("div#content", 0);
echo $selector->innertext;
//it outputs "Hello World. Text I don't want to select. Text I don't want to select"
//but
I want only "Hello World"
include_once('simple_html_dom.php');
$html = new simple_html_dom();
$text = '<div id="content">
Hello World.
<div id="sub-content1">
Text I don\'t want to select.
</div>
<div id="sub-content2">
Text I don\'t want to select
</div>
</div>';
$html->load($text);
$selector =$html->find("div#content",0)->find("*");
foreach($selector as $node){
$node->outertext = '';
}
$html->load($html->save());
$selector =$html->find("div#content",0);
echo $selector->innertext;
I wish to replace tags from a WYSIWYG editor to .
At the moment I am using the following code to achieve this.
$content = preg_replace('/<h1(.*?)<\/h1>/si', '<p class="heading-1"$1</p>', $content);
$content = preg_replace('/<h2(.*?)<\/h2>/si', '<p class="heading-2"$1</p>', $content);
$content = preg_replace('/<h3(.*?)<\/h3>/si', '<p class="heading-3"$1</p>', $content);
$content = preg_replace('/<h4(.*?)<\/h4>/si', '<p class="heading-4"$1</p>', $content);
$content = preg_replace('/<h5(.*?)<\/h5>/si', '<p class="heading-5"$1</p>', $content);
$content = preg_replace('/<h6(.*?)<\/h6>/si', '<p class="heading-6"$1</p>', $content);
As you can see this code is quite messy, it would be great if I could condense this into a single regular expression but I simply lack the ability to do so.
I had considered this line of code as an alternative.
$content = preg_replace('/<h(.*?)<\/h(.*?)>/si', '<p class="heading-$2"$1</p>', $content);
I'm not sure about using the above, clients have a tendency to copy content from other sites, paste it straight into their new WYSIWYG and i've seen anything from hr tags to heading tags popping in there.
What I require is simply the above single line, except the tag itself can only be 2 specific characters (So ensure the tag starts with a H and is followed by [1-6]).
I also require that the class it adds to the p tag is specific to the number use, eg: heading-1, heading-2.
Any help would be greatly appreciated, thank you for your time.
$content = <<<HTML
<h1 class="heading-title">test1</h1>
<H2 class="green">test2</H2>
<h5 class="red">test</h5>
<h5 class="">test test</h5>
HTML;
$content = preg_replace('#<h([1-6]).*?class="(.*?)".*?>(.*?)<\/h[1-6]>#si', '<p class="heading-${1} ${2}">${3}</p>', $content);
echo htmlentities($content);
Result:
<p class="heading-1 heading-title">test1</p>
<p class="heading-2 green">test2</p>
<p class="heading-5 red">test</p>
<p class="heading-5 ">test test</p>
Note for existing classes:
Even if your element doesn't have an existing class you have to add empty class attribute class="". Instead this will not work as expected. :( Better solution is to use preg_replace_callback. Then you can check if a match exists and create your p tags more accurately.
$content = <<<HTML
<h1 style="heading-title" attr="attr" label="label">test1</h1>
<h1>test</h1>
<H2 style="heading-title" class="green">test2</H2>
<h5 style="heading-title" class="red">test</h5>
<h2 style="bbb" class="a" style="dazd-title" >test</h2>
HTML;
$content = \preg_replace('#<h([1-6])\s*((?:(?!class).)*?)>(.*?)</h[1-6]>#si', '<p class="heading-${1}" ${2}><span style="color: #e03e2d">${3}</span></p>', $content);
$content = \preg_replace('#<h([1-6])(.*?)class="(.*?)"(.*?)>(.*?)<\/h[1-6]>#si', '<p ${2} class="heading-${1} ${3}" ${4}><span style="color: #e03e2d">${5}</span></p>', $content);
echo ($content);
Result:
<p class="heading-1" style="heading-title" attr="attr" label="label">test1</p>
<p class="heading-1" >test</p>
<p class="heading-2 green">test2</p>
<p class="heading-5 red">test</p>
<p class="heading-2 ">test</p>