I did a regular expression search and replace and it's suppose to be completed but I found a flaw and I'm stuck.
What my search and replace does is, file the 'title' attributes in html tags and convert them to a mouseover javascript. e.g:
Code:
<a href="http://website.com" title="That's website">
<a href='http://another.com' title='This website'>
<hr title="Horizontal Ruler" />
<hr title="No space"/>
I hope you understand the differences in the 4 tags (double quote, single quote, space and slash). My search and replace will make those tags into these:
Code:
<a href="http://website.com" onMouseOver="overlib('That\'s website')">
<a href='http://another.com' onMouseOver="overlib('This website')">
<hr onMouseOver="overlib('Horizontal Ruler')" />
<hr onMouseOver="overlib('No Space')"/>
It works well but the flaw is, when there is an empty title attributes,
Code:
<a href="http://empty.com" title="">Empty</a><a href="http://null.com">null</a>
It will become this :-
Code:
<a href="http://empty.com" onMouseOver="overlib('\">Empty</a><a href=')">"http://null.com">null</a>
Why?? Because it assumed in the
title="", the second quote is the content which it should take, up to the next quote that arrive, which is href=".
Here's my regular expression code that I've made :-
PHP Code:
$search = '/title=[\"|\'|](.+?)[\"|\'|](\s|\>|\/)/is';
$replace = 'onmouseover="overlib(\' ' . addslashes('$1') . ' \')" $2';
$content = preg_replace($search,$replace,$content);
$1 is the content inside the title attribute and $2 is the end part of it, be it a space, closing angle bracket (>) or a slash.
To further explain my regular explression, the [\"|\'|] will look for the quote in the attributes. The (.+?) gets the content in between the next [\"|\'|]. And the (\s|\>|\/) looks for space, closing angle bracket (>) or slash.
Thanks for your help.. 