LittleManiac
LittleManiac

Reputation: 3

Getting the content of a paragraph

I want something like this

<p id="potato">Here's some text</p>

<?php
SomeSortOfCodeToGetTheContentOfParagraph(#potato)
$value = TheOutPutOfTheCodeAbove
?>

So basically I want a code that gets the content of the paragraph with ID potato and makes $value equal to that

I read something about DOM but I don't really understand that

It doesn't have to be a PHP code, as long as $value is PHP and can be defined that way

Upvotes: 0

Views: 44

Answers (3)

Ohgodwhy
Ohgodwhy

Reputation: 50797

You can do this using the DOMDocument class in PHP. Although I'm not sure where you're getting that string from, a quick mockup would be:

$doc = new DOMDocument();
$doc->loadHTML('<p id="potato">Here\'s some text</p>');
foreach($doc->childNodes as $item):
    if($item->nodeType == 1):
        echo $item->nodeValue;
    endif;
endforeach;

Upvotes: 0

Diego Romero
Diego Romero

Reputation: 130

PHP is a server side language, that means all the code you write evaluates before generating the DOM, you can't manipulate DOM like you want after it has been rendered. To do that you need to use javascript which is executed on the DOM, with php you can't basically do what you're trying to achieve, what you can do is retrieve the text with JS and pass it to PHP with GET or POST methods.

Upvotes: 1

David Jones
David Jones

Reputation: 4305

jQuery would achieve this. You could use:

$('#potato').text();

You should read the documentation to get a better grasp of how you can use it

Upvotes: 0

Related Questions