PHP: How to add a CSS text-align property to an already present inline style attribute of an element using simple_html_dom parser?


Tags: javascript,php,html,css,simple-html-dom

Problem :

I have a string variable $str which contains HTML for a part of a page. I need to add the CSS property text-align:justify to all the <p> elements in the HTML (preferably using simple_html_dom parser).

Some of the <p> elements already have an inline style attribute, others don't. So I can not do something like

$str_html = str_get_html($str);
foreach ($str->find('p') as $p) {
  $p->style='text-align:justify;';
}

Because this will void any other styles already applied to the <p> element.

PS:- I can not use JQuery. I can use JS but will only use it as a last resort.



Solution :

You could concatenate instead of re-assigning

$str_html = str_get_html($str);
foreach ($str->find('p') as $p) {
  $p->style .= '; text-align:justify;';
}

    CSS Howto..

    How to set icon of an page using CSS

    How can I show a DIV when hovering on a DIV using pure CSS?

    How to maximum size without exceed the size?

    How to close this menu clicking outside?

    How To Select Two classes with CSS?

    How to make emails responsive?

    jQueryUI show/hide animation issue with child nodes

    How php execute in file with extension .ctp in cakephp?

    How to get css div to stretch to 100% of window

    How do I make this type of layout with CSS? [closed]

    How to make a menu have submenus in pure CSS

    How to write css code in javascript? (Uncaught TypeError: Cannot set property “height” of undefined)

    How can I do to merge font attributes in css

    how to align divs vertically in html and css?

    How to ignore parent css style

    How do I create triangles like this? [closed]

    HTML & CSS: How to create four equal size tabs that fill 100% width?

    How to implement geometric div backgroud shapes in css

    How to make the css affect the new content loaded by jquery?

    How to make an equal distance between elements footer?

    How can I add pagination to my site's gallery?

    How to change and maintain multiple color themes at runtime with css?

    HTML CSS How to stop a table to put the content after it on a new line?

    How to hide hr inside the last li using css

    How to set position of checkbox to middle of td and input type text using css? [duplicate]

    How to add the img-responsive class in javascript

    how to write css to reduce font size [duplicate]

    How do I center a button in html5?

    How to remove a element after animation?

    how to position two transparent images to overlap opposite corners of a container div