Regular Expression problem
I have Regular Expression code handling graphic files.
$view[content] = preg_replace("/(\<img )([^\>]*)(\>)/i", "\\1 name='target_resize_image[]' onclick='image_window(this)' style='cursor:pointer;' \\2 \\3", $view[content]);
At web pages, if there is not ‘style” on html code, this code works fine. But if there is “style”, the “style” code was changed into “style=’cursor:pointer;”.
I wnat, if there is “style=’…'” at img, the style=’…'” is added. If not, the “style” code should be “style=’cursor:pointer;'”.
The “preg_replace” gets rid of “style=’aaaaaa'” at img code. That should be “style=’cursor:pointer'”.
code input
<img style="border-bottom: medium none; border-left: medium none; width: 160px; float: left; height: 160px; border-top: medium none; margin-right: 1em; border-right: medium none" alt="120131_12e1c8be2d6954ec9a3579ae57a64bfe_3EsTQWri2Zx9.gif" src="/data/cheditor4/1201/120131_02133169e006e1d08fc72fa5ff1e7a25_5KKOd8zrZhluXoqpiN.gif" />
code output
<img style="cursor:pointer" alt="120131_12e1c8be2d6954ec9a3579ae57a64bfe_3EsTQWri2Zx9.gif" src="/data/cheditor4/1201/120131_02133169e006e1d08fc72fa5ff1e7a25_5KKOd8zrZhluXoqpiN.gif" />
code – should be
<img style="cursor:pointer;border-bottom: medium none; border-left: medium none; width: 160px; float: left; height: 160px; border-top: medium none; margin-right: 1em; border-right: medium none" alt="120131_12e1c8be2d6954ec9a3579ae57a64bfe_3EsTQWri2Zx9.gif" src="/data/cheditor4/1201/120131_02133169e006e1d08fc72fa5ff1e7a25_5KKOd8zrZhluXoqpiN.gif" />
Any helpful comment would be appreciated.
Your regexp creates this output:
Please note, that you have -of course- two style attributes in this string. Apparently you use some kind of HTML validation, which automatically “fixes” this by removing the 2nd one and just leaves you with
style='cursor:pointer;'.You could improve your regexp. Using a pattern like
/(alt|style|src)=("[^"]*")/iwithpreg_match_allwill allow you to extract the attributes of yourimgtag, then manipulate them and build a new HTML string from it.Anyway, I do not recommend manipulating HTML using RegExp’s at all. It is much more simple and robust to Use DOM tools. Have a look at the Simple HTML DOM Parser. This simple code
gives you the output
Voila! And it doesn’t depend on the order of your attributes, on used quotes, spaces and line-breaks.
EDIT: WindStory has requested a non-DOM Solution
The solution depends very much on the given strcuture of your input string. If you, for example, know for sure, that every given
imgalready has astyleattribute, you can do it with a plain replace:Having less guarantees on your given tag, you can use the RegExp I mentioned above to extract the attributes from the img tag
See it in action here.
Hope that helps.
EDIT 2: WindStory has asked for a more fault-tolerant RegExp solution
Here is a small function, to add/change attributes in a given HTML string. It supports single or double quotes for attribtues, but no missing quotes and it will not work, if a single quote is enclosed by double quotes and vice versa.
Insert a html-Tag as
$html, define the$nameof the attribute and define the$value. If the attribute is already present, the value will be replaced, otherwise it will be added. If you set$append, the$valuewill be appended and$appendwill be used as concatenation sign.This
will outout