Hi, i am new to PHP but i have some common knowledge to recognizing commands and such.


in my page i have code like this

<div class="page_column page_column_first" id="page_column_1" style="width: 499px;"><div class="page_block_container" id="page_block_759"><div class="disignBoxFirst">
    <div class="boxFirstHeader"><div class="dbTitle">Up and Coming</div></div>

    <div class="boxContent"><div class="dbContentHtml"><p>This is a placeholder for up and coming news to be edited at News Time.</p></div></div>

So whatever class it is linked to say "boxContent" which is the name of a section on a .css file on the server, i want it take the information under that header and place it on that same line and doing something like

preg_replace

class="boxContent with style="border: 0px;
background-color: transparent;
margin:10px auto 5px;"


Can somebody write something like that for me please or help me with it? It's basically taking the css rule and converting it to inline Thanks

A preg will not suffice, as there can be more classes separated by a space. Have a look at preg_replace_callback. You pattern will look something like this:

/ class="(.*?)"/

Could some more code in detail because i tried that code and it didn't work really, it just replaced:

class="*" with style="*" not even with the actual information inside it. I don't mind about the spaces. As long as it stays in the quotations marks. Please?

<?php
  $html = '<div class="page_column page_column_first" id="page_column_1" style="width: 499px;">' .
          '<div class="page_block_container" id="page_block_759"><div class="disignBoxFirst">' .
          '<div class="boxFirstHeader"><div class="dbTitle">Up and Coming</div></div>' .
          '<div class="boxContent"><div class="dbContentHtml">' .
          '<p>This is a placeholder for up and coming news to be edited at News Time.</p></div></div>';
  $pattern = '/ class="(.*?)"/';
  
  function styleReplace($matches) {
    $css['page_column'] = 'font-color: #000;';
    $css['page_column_first'] = 'font-style: italic;';
    
    $classes = explode(' ', $matches[1]);
    $style = ' style="';
    foreach ($classes as $class)
      $style .= $css[$class];
    
    $style .= '"';
    return $style;
  }
  
  echo preg_replace_callback($pattern, 'styleReplace', $html);
?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.