Это может работать для ваших нужд:
$text = '<p id="paragraph" class="green">This is a paragraph with an image <img src="/path/to/image.jpg" width="50" height="75"/></p>';
echo preg_replace("/<([a-z][a-z0-9]*)(?:[^>]*(\ssrc=['\"][^'\"]*['\"]))?[^>]*?(\/?)>/i",'<$1$2$3>', $text);
// <p>This is a paragraph with an image <img src="/path/to/image.jpg"/></p>
RegExp разбит:
/ # Start Pattern
< # Match '<' at beginning of tags
( # Start Capture Group $1 - Tag Name
[a-z] # Match 'a' through 'z'
[a-z0-9]* # Match 'a' through 'z' or '0' through '9' zero or more times
) # End Capture Group
(?: # Start Non-Capture Group
[^>]* # Match anything other than '>', Zero or More Times
( # Start Capture Group $2 - ' src="...."'
\s # Match one whitespace
src= # Match 'src='
['"] # Match ' or "
[^'"]* # Match anything other than ' or "
['"] # Match ' or "
) # End Capture Group 2
)? # End Non-Capture Group, match group zero or one time
[^>]*? # Match anything other than '>', Zero or More times, not-greedy (wont eat the /)
(\/?) # Capture Group $3 - '/' if it is there
> # Match '>'
/i # End Pattern - Case Insensitive
Добавьте несколько цитат и используйте заменяющий текст <$1$2$3>
.src=
свойств из правильно сформированных HTML-тегов.
Обратите внимание Это не обязательно сработает при вводе ALL , как Anti-HTML + RegExpлюди так ловко отмечают ниже.Есть несколько откатов, в частности <p style=">">
может закончиться <p>">
и несколько других неработающих проблем ... Я бы рекомендовал посмотреть Zend_Filter_StripTags как фильтр тегов / атрибутов полного доказательства в PHP