0

i need to replace all div's containig class "ta" and id "ta_somenumber" with textareas that will keep same attributes. Here is example code:

$html_content = '<div class="ta" id="ta_345">sometext</div><span style="...">Some text</span><--!more html--><div class="ta" id="ta_5687">sometext</div>';

Here is what i want to achieve:

$html_new_content = '<textarea class="ta" id="ta_345">sometext</textarea><span style="...">Some text</span><--!more html--><textarea class="ta" id="ta_5687">sometext</textarea>';

I was trying with this:

$regex1 = '#\<div class=\"ta\" id=\"(.*)\"\>(.+?)\<\/div\>#s';
$regex2 = '#\<textarea class=\"ta\" id=\"(.*)\"\>(.+?)\<\/textarea\>#s';

$result = str_replace($regex2, $regex3, $html_content);

But for some reason this doesnt work. I tried with preg_replace but no luck.

SomeoneS
  • 1,207
  • 2
  • 19
  • 34
  • 1
    Just a note that it's bad practice to try to parse HTML using regex, as HTML is not a regular language. http://stackoverflow.com/questions/590747/using-regular-expressions-to-parse-html-why-not – Matt Jul 31 '12 at 19:05

1 Answers1

2

You only need one regex, and that would look like this:

$regex = '#<div class="ta" id="([^"]+)">(.+?)</div>#s';
$result = preg_replace( $regex, 
    '<textarea class="ta" id="$1">$2</textarea>', 
    $html_content
);

This finds all of the <div> tags, capturing their ID attributes in backreference #1 and their content in backreference #2. Then, the replacement is simple: Replace the <div> with a <textarea> that has the "ta" class and the same ID and contents as the original <div>.

You can see it working in the demo.

nickb
  • 59,313
  • 13
  • 108
  • 143
  • But only if the `class` and `id` attribute are always ordered the same way, seperated by a single space, uses `"` and not `'` to delimit the attribute values, there are no other attributes etc... @SomeoneS : you should rather create a tree / DOM and change the attributes there. – amon Jul 31 '12 at 19:10