0

I want to replace this line :

<img width="600" height="256" alt="javascript00" src="http://localhost/img/test.png" title="javascript00" class="aligncenter size-full wp-image-1973">

With this :

<p align="center"><img width="600" height="256" alt="javascript00" src="http://localhost/img/test.png" title="javascript00"></p>

By use a simple regexp. It consists to delete the image class and add a <p align="center"> around only when the img class contain aligncenter :)

Thanks for help! And merry christmas :)

Solution :

$result = preg_replace('#<img([^>]*?)?\s+class="[^"]*aligncenter[^"]*"\s*([^>]*?)>#', '<p align="center"><img$1 $2></p>', $data);
CrazyMax
  • 781
  • 1
  • 11
  • 24

2 Answers2

2

This should work, although it is not advisable to parse and manipulate HTML with regular expressions.

<?php
$in = '<img width="600" height="256" alt="javascript00" src="http://localhost/img/test.png" title="javascript00" class="aligncenter size-full wp-image-1973">';

$out = preg_replace(
    '@<img( [^>]*?)\s*class="[^"]*"([^>]*?)>@', 
    '<p align="center"><img $1$2></p>', 
    $in
);

// if you need the image's class to be replaced with one class:
$out = preg_replace(
    '@<img( [^*]+?)\s*class="[^"]*"([^>]*?)>@', 
    '<p align="center"><img class="aligncenter" $1$2></p>', 
    $in
);

There are other questions and answers on here that deal with the issue of why you should not use regexes to parse and manipulate HTML (which should be required reading before SO lets you create an account).

Assuming you are dealing with HTML you are retrieving from an external source that you have no control over, you would use DOMDocument's loadHTML method and suppress errors (if you have no control over the markup, this will handle quite malformed HTML, but it likes to emit errors even when it builds the document up just fine, so use @)

$dom = new DOMDocument;
// supress errors because DOMDocument will actually parse a malformed document
// even when it emits errors. this is something that is wrong with PHP. 
@$dom->loadHTML('<img src="foo" class="bar">');
$xp = new DOMXPath($dom);
$node = $xp->query('body/img')->item(0);
$node->removeAttribute('class');
echo $dom->saveXML($node).PHP_EOL;
Community
  • 1
  • 1
Shabbyrobe
  • 12,298
  • 15
  • 60
  • 87
  • I don't want a `class="aligncenter"` form my img tag, i want to add `

    ` around each img tag containing this class and delete this one. To resume, remove class of the img tag and add `

    ` :)

    – CrazyMax Dec 26 '10 at 13:16
  • Your first regex does'nt work with this syntax : `` – CrazyMax Dec 26 '10 at 13:51
0

If you need this just for img tags, you can use this regexp:

  $data = '<img width="600" height="256" alt="javascript00" src="http://localhost/img/test.png" title="javascript00" class="aligncenter size-full wp-image-1973">';
  $result = preg_replace('#<img( [^>]*?)\s+class="[^"]*"\s*([^>]*?)>#', '<p align="center"><img $1$2></p>', $data);
  echo $result;
StasM
  • 10,593
  • 6
  • 56
  • 103
  • Almost identical to my answer, 8 minutes later. Why? – Shabbyrobe Dec 26 '10 at 01:23
  • @Shabbyrobe when I saw your answer, the regexp was wrong and the rest wasn't even there. Also, it takes time to type the answer, so when it's committed is not when I started to write it. – StasM Dec 26 '10 at 01:35
  • Ok, cool, I understand. Though I disagree that the initial regex was wrong, just more broad. It certainly worked with the OP's question. – Shabbyrobe Dec 26 '10 at 01:45
  • @StatsM : Hummm doesn't work with this syntax : `` – CrazyMax Dec 26 '10 at 13:15
  • @StatsM extra space? There is only one space between tags in img. The problem in your regex is the class must be at least the second tag. When is it the first like `` it doesn't work. – CrazyMax Dec 26 '10 at 14:20
  • I finally found : `$result = preg_replace('#]*?)?\s+class="[^"]*aligncenter[^"]*"\s*([^>]*?)>#', '

    ', $data);`
    – CrazyMax Dec 26 '10 at 14:30