View Single Post
  #1 (permalink)  
Old 01-08-06, 09:19 PM
eddyvlad eddyvlad is offline
Wannabe Coder
 
Join Date: Sep 2003
Location: In The Bloody Pits Of Hell
Posts: 160
Thanks: 2
Thanked 0 Times in 0 Posts
Help with regular expression... :)

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..
__________________
Mr. Brown Eyes

Last edited by eddyvlad; 01-08-06 at 09:22 PM.
Reply With Quote