Current location: Hot Scripts Forums » Programming Languages » PHP » Help with regular expression... :)

Help with regular expression... :)

Reply
  #1 (permalink)  
Old 01-08-06, 08:19 PM
eddyvlad eddyvlad is offline
Wannabe Coder
 
Join Date: Sep 2003
Location: In The Bloody Pits Of Hell
Posts: 156
Thanks: 1
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 08:22 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 01-08-06, 09:18 PM
End User's Avatar
End User End User is offline
Level II Curmudgeon
 
Join Date: Dec 2004
Posts: 3,029
Thanks: 14
Thanked 34 Times in 33 Posts
I'm not positive, but I think you may need to drop the "?", like this:

/title=[\"|\'|](.+)[\"|\'|](\s|\>|\/)/

This will only do the S&R if there is at least one char between the quotes in the title tag. I haven't tested this but unless I've misunderstood you, this should work.
__________________
I don't live on the edge, but sometimes I go there to visit.
-------------------------------------------------------------------------
Sanitize Your Data (scroll down)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 01-08-06, 10:10 PM
eddyvlad eddyvlad is offline
Wannabe Coder
 
Join Date: Sep 2003
Location: In The Bloody Pits Of Hell
Posts: 156
Thanks: 1
Thanked 0 Times in 0 Posts
That's what I was thinking, it's suppose to work. But it didn't.
My page went haywire probably because I'm using the /e modifier so that I can use addslashes().
What happened when I removed the ? was, it adds \\\" to every quote in my page e.g
<img src=
\\\"image.jpg\\\" />

I haven't figured out why it did that but I tried removing /e modifier and addslashes(), the replacement became e.g:
<a href="website.com" onMouseOver="
overlib('Something something>Webby</a>.

Yet, that doesn't solve..
__________________
Mr. Brown Eyes
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Regular Expression help zoic PHP 4 12-02-05 06:15 PM
regular expression for parsing url link musmanm80 Perl 1 07-27-05 11:58 PM
Regular Expression KHWright Visual Basic 0 06-02-05 06:40 PM
HELP!!! - Invalid preceding regular expression CMIVXX PHP 1 01-04-05 03:55 AM
suggest a regular expression gmadhukarreddy Perl 2 01-08-04 12:56 AM


All times are GMT -5. The time now is 01:05 AM.
vBulletin® Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.