Asked  7 Months ago    Answers:  5   Viewed   34 times
<blockquote>
 <p>
   2 1/2 cups sweet cherries, pitted<br>
   1 tablespoon cornstarch <br>
   1/4 cup fine-grain natural cane sugar
 </p>
</blockquote>

hi , i want to get the text inside 'p' tag . you see there are three different line and i want to print them separately after adding some extra text with each line . here is my code block

    $tags = $dom->getElementsByTagName('blockquote');
    foreach($tags as $tag)
    {
        $datas = $tag->getElementsByTagName('p');
        foreach($datas as $data)
        {
            $line = $data->nodeValue;
            echo $line;
        }
    } 

main problem is $line contains the full text inside 'p' tag including 'br' tag . how can i separate the three lines to treat them respectively ??

thanks in advance.

 Answers

25

You can do that with XPath. All you have to do is query the text nodes. No need to explode or something like that:

$dom = new DOMDocument;
$dom->loadHtml($html);
$xp = new DOMXPath($dom);
foreach ($xp->query('/html/body/blockquote/p/text()') as $textNode) {
    echo "n<li>", trim($textNode->textContent);
}

The non-XPath alternative would be to iterate the children of the P tag and only output them when they are DOMText nodes:

$dom = new DOMDocument;
$dom->loadHtml($html);
foreach ($dom->getElementsByTagName('p')->item(0)->childNodes as $pChild) {
    if ($pChild->nodeType === XML_TEXT_NODE) {
        echo "n<li>", trim($pChild->textContent);
    }
}

Both will output (demo)

<li>2 1/2 cups sweet cherries, pitted
<li>1 tablespoon cornstarch
<li>1/4 cup fine-grain natural cane sugar

Also see DOMDocument in php for an explanation of the node concept. It's crucial to understand when working with DOM.

Wednesday, March 31, 2021
 
KouiK
answered 7 Months ago
77

A quick search on the subject reveals this (incorrect) bug report:

http://bugs.php.net/bug.php?id=46136

The last reply states the following:

You're using HTML 4 rules to load an XHTML document. Either use the load() method to parse as XML or the libxml_use_internal_errors() function to ignore the warnings.

I can't be sure if you are encountering this problem for the same reasons, since you did not include a reference to the HTML page being loaded. In any case, using libxml_use_internal_errors() should at least suppress the error.

ID's in HTML documents are generally unique, so the best solution would still be validating your document, if at all possible.

Wednesday, March 31, 2021
 
rasmusx
answered 7 Months ago
28

Change the user_agent value in php.ini, which should be sent in anything making use of the http stream wrapper like DOMDocument::loadHtmlFile(), file_get_contents(), etc.

$fake_user_agent = "Mozilla/5.0 (X11; Linux i686) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.47 Safari/536.11";
ini_set('user_agent', $fake_user_agent);

The same can also be accomplished in an Apache .htaccess by setting php_value user_agent if permitted by your server configuration.

Saturday, May 29, 2021
 
Powering
answered 5 Months ago
12

Well I have implemented by this way:

LinearLayout linearLayout = (LinearLayout)findViewById(R.id.layout);
ViewTreeObserver viewTreeObserver = linearLayout.getViewTreeObserver(); 
viewTreeObserver.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { 
    @Override 
    public void onGlobalLayout() { 
        linearLayout.getViewTreeObserver().removeGlobalOnLayoutListener(this); 
        int width  = linearLayout.getMeasuredWidth();
        int height = linearLayout.getMeasuredHeight(); 

    } 
});

This example is for Linear layout, for Relative layout follow same process.

Hope this would help you.

Friday, July 30, 2021
 
TheFrack
answered 3 Months ago
21

Why don't you just write a simple code like this (not the best solution, for sure, just an example based on the help article):

double slope(const vector<double>& x, const vector<double>& y){
    if(x.size() != y.size()){
        throw exception("...");
    }
    size_t n = x.size();

    double avgX = accumulate(x.begin(), x.end(), 0.0) / n;
    double avgY = accumulate(y.begin(), y.end(), 0.0) / n;

    double numerator = 0.0;
    double denominator = 0.0;

    for(size_t i=0; i<n; ++i){
        numerator += (x[i] - avgX) * (y[i] - avgY);
        denominator += (x[i] - avgX) * (x[i] - avgX);
    }

    if(denominator == 0.0){
        throw exception("...");
    }

    return numerator / denominator;
}

Note that the third argument of accumulate function must be 0.0 rather than 0, otherwise the compiler will deduct its type as int and there are great chances that the result of accumulate calls will be wrong (it's actually wrong using MSVC2010 and mingw-w64 when passing 0 as the third parameter).

Thursday, August 12, 2021
 
Shafeeq Mohammed
answered 2 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :