Retrieve inline CSS attributes with PHP

Asked

Viewed 65 times

0

I have DIV’s that have dynamically generated CSS inline styles.

Example: <div style="top: 275px; left: 563px;"></div>

My doubt is the following, how can I recover these values "top" and "left" with PHP/Codeigniter so that I can register in my database later?

  • I removed the CSS tag because, in my view, the issue is not exactly related to CSS.

  • How dynamically these attributes are generated?

  • Hey Sam, good morning Master. You want to recover in render time / in form submission or through action type one click on button?

  • These Divs are generated every time the user clicks on an image on the screen, these Divs are generated with the inline CSS attributes "top" and "left" referring to the click position.

1 answer

0

If you can print an ID for each of these Divs, you could try something like this:

function explodeAttributes($string) {
    return explode(':', $string);
}

$domDoc = new DOMDocument();
$html_string = '<div id="div1" style="top: 275px; left: 563px;"></div>';

//methods to load HTML
$domDoc->loadHTML($html_string);
$documentElement = $domDoc->getElementById('div1')->getAttribute('style'); // pega o atributo style
$documentElement = explode(';',$documentElement); // converte os atributos para um array
$documentElement = array_filter($documentElement); // remove os itens vazios do array
$documentElement = array_map("explodeAttributes", $documentElement); // converte as propriedades do CSS para array

echo "<pre>" ;
print_r($documentElement);

The result would be something like

Array
(
    [0] => Array
        (
            [0] => top
            [1] =>  275px
        )

    [1] => Array
        (
            [0] =>  left
            [1] =>  563px
        )

)

Online Test:

http://sandbox.onlinephpfunctions.com/code/f4f9b95a254ee0546c6a4a152ed0f5d9ac729fc8

Browser other questions tagged

You are not signed in. Login or sign up in order to post.