Si un div tiene la clase "uno-dos-tres", entonces

I'm currently working on a website. And I need to use PHP to select a div with a certain class. For example:


<div class="header"></div>
<div class="other-class"></div>
<div class="one-two-three"></div>
<div class="footer"></div>


        //function code here...

But keep in mind that I want to use PHP and not jQuery for this...

preguntado el 24 de agosto de 12 a las 09:08

What do you want to do then? It seems that php is not the right tool... -

That's strange and probably not possible. What do you want to be able to do exactly? Why do you want to process the div contents with php after it's been sent to the browser? -

you can't: php is server side, JavaScript is where you should be looking. Besides, your PHP code just looks like jQuery, in any case it's not valid php -

I know that the code is not valid but it shows what I want. I was just wondering if this was possible... -

This is an odd request, what are you actually trying to do? PHP isn't a Javascript alternative. It's kind of like saying I want to use PHP and not CSS for this – Server Side (PHP/Python/Ruby/ etc) + JS + CSS + HTML = Web Page – typically you'll need bits from each of the 4. -

3 Respuestas

If you want do manipulate the DOM, prior to sending it to the client then the Dom object is offers what you need. It even has similar methods to what you might know already from JS.

$htmlString = '<html>...</html>';//either generated, or loaded from a file
$dom = new DOMDocument;
$dom->loadHTML($htmlString);//or loadHTMLFile
$divs = $dom->getElementsByTagName('div');
foreach($divs as $div)
    if ($div->hasAttribute('class') && strstr($div->getAttribute('class'), 'one-two-three'))
        //$div ~= <div class='one-two-three'>
        $div->appendChild();//check the docs to see what you can do

Aquí hay un overview of the methods a su disposición

Respondido 24 ago 12, 10:08

strstr will also match if the element has a class one-two-three-four. Would probably need to tokenize it first. - Dmitri Pisarev

You probably will fill the div with content using php and then you can process your content first and then print it in the div. If you need to do stuff to the div's contents when the div's already in the browser you will need to use javascript to do it or else call a php script to do the processing using ajax to call the php script on the backend and retrieve the response.

Respondido 24 ago 12, 09:08

use simplephpdom library for this. You can select form elements with selectors like you do in jquery.

Respondido 24 ago 12, 09:08

Thanks for this link. I think I am going to use jQuery after all. But I will look into this. Thanks! - Guido Visser

Didn't know that existed but seems like a strange thing to me anyway, unnecessary kind of library. If somebody knows a good application of this kind of thing I'd be interested to hear it! - asciiom

well this library is very useful if you want web scraping easily. - WatsMyName

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.