这可能会为您的工作需要:
$text = '
This is a paragraph with an image
echo preg_replace("/]*(\ssrc=['\"][^'\"]*['\"]))?[^>]*?(\/?)>/i",'', $text);
//
This is a paragraph with an image
正则表达式细分:
/ # Start Pattern
< # Match '
( # 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
添加一些报价,并使用替换文本应该从良好剥离任何非src=性质形成了HTML标签。
请注意这不一定要去上ALL投入工作,因为反HTML + RegExp的人都是这样巧妙下面值得注意。在PHP