The Cache: Technology Expert's Forum
 
*
Welcome, Guest. Please login or register. September 19, 2019, 05:21:41 AM

Login with username, password and session length


Pages: [1]
  Print  
Author Topic: php regex problem getattribute Nutballs?  (Read 2548 times)
nattsurfaren
Journeyman
***
Offline Offline

Posts: 64


View Profile
« on: November 13, 2008, 05:18:24 PM »

I have been using this function for a time now but I found out hat it doesnt work when an attribute is empty.
Nutballs where are you?  Grin
I have been trying to use http://regexpal.com/ but now it doesn't work for some reason.
Need really help on this.
Sincerely
Natt

Code:
function getAttribute($attrib, $tag)
{
    //get attribute from html tag

    $re = '/' . preg_quote($attrib) . '=([\'"])?((?(1).+?|[^\s>]+))(?(1)\1)/is';
if (preg_match($re, $tag, $match))
    {
        return urldecode($match[2]);
    }
    return false;
}
Logged
vsloathe
vim ftw!
Global Moderator
Lifer
*****
Offline Offline

Posts: 1669



View Profile
« Reply #1 on: November 13, 2008, 09:50:46 PM »

Bah, what a bloated mess. Negative lookaheads and lookbehinds increase your overhead like crazy.

Use PHP's DOM. It's way better suited to do what you want to do.

If you insist on using regex though, I will pretend to be a competent programmer/sysadmin for a moment...

Code:
<?php
function getAttribute($attrib$tag)
{
    
//get attribute from html tag

    
$re '/' preg_quote($attrib) . '=(?:[\'\"])?([^\'\"]*?)(?:[\'\"])?/is';
if (preg_match($re$tag$match))
    {
        return 
urldecode($match[1]);
    }
    return 
false;
}
?>



Vomit

The above uses non-capturing groups for the quotes, and escapes double quotes which is something you really ought to do when using PCRE.

Also your regex is unnecessarily bloated. There's no need to look for anything other than "not a quote, repeated 0 or more times" in your grouping expression.
Logged

hai
nattsurfaren
Journeyman
***
Offline Offline

Posts: 64


View Profile
« Reply #2 on: November 14, 2008, 08:02:01 AM »

Thanks vsloathe for your help.

Quote
Also your regex is unnecessarily bloated

Luckily I didn't create the function so It's not my fault  ROFLMAO

Thanks again vsloathe  Wink
Logged
nattsurfaren
Journeyman
***
Offline Offline

Posts: 64


View Profile
« Reply #3 on: November 14, 2008, 09:10:06 AM »

Hmm I'm having problem.
I think it is when there is no ' or ".

Logged
vsloathe
vim ftw!
Global Moderator
Lifer
*****
Offline Offline

Posts: 1669



View Profile
« Reply #4 on: November 14, 2008, 12:39:36 PM »

Hmm I'm having problem.
I think it is when there is no ' or ".



That doesn't seem likely, as that grouping is optional (followed by a ? ).
Logged

hai
nattsurfaren
Journeyman
***
Offline Offline

Posts: 64


View Profile
« Reply #5 on: November 16, 2008, 11:02:21 AM »

Hmm I'm having problem.
I think it is when there is no ' or ".



That doesn't seem likely, as that grouping is optional (followed by a ? ).

Hi vsloathe I was wrong with the attrib without ' ". Sorry about that Smiley . But this time I think I found why some attributes fail.
Here is a sample of an input tag
Code:
input type="hidden" name="namespaceMapping" value='&lt;prefixNode xmlns:prs="http://webplatform.rug.nl/_definition/classes/person"
xmlns:s="http://webplatform.rug.nl/_definition/shared/schemas/shared.xsd"
xmlns:ssrch="http://webplatform.rug.nl/_definition/classes/simpleSearch"
xmlns:sfr="http://webplatform.rug.nl/_definition/classes/simpleFormResults"
xmlns:sels="http://webplatform.rug.nl/_definition/classes/selectionSearch"
xmlns:o="http://webplatform.rug.nl/_definition/classes/order"/&gt;'

The problem occure when grabbing 'value'.
I think the problem is because the mix of ' and ".
When starting with ' it should end with ' and not ".

Thanks for trying to help me vsloathe.
Now I'm going to grab a cup of coffee because I have much scripting to do  Wink
Logged
Pages: [1]
  Print  
 
Jump to:  

Perkiset's Place Home   Best of The Cache   phpMyIDE: MySQL Stored Procedures, Functions & Triggers
Politics @ Perkiset's   Pinkhat's Perspective   
cache
mart
coder
programmers
ajax
php
javascript
Powered by MySQL Powered by PHP Powered by SMF 1.1.2 | SMF © 2006-2007, Simple Machines LLC
Seo4Smf v0.2 © Webmaster's Talks


Valid XHTML 1.0! Valid CSS!