Jump to content

Conditional subpattern


lordfrikk

Recommended Posts

Hey,

 

I was working on kinda complex regex lately, I have it done now, but I was wonderin if there was a way to minify the code.

 

Here's that I was pondering about:

 

Sample text:

 

<a href="LINK">LINK</a>

 

And sample pattern:

 

_(?:<a href=")?(LINK)(?:">)?/1(?:<\/a>)?_i

 

Explanation:

 

I only wanna grab LINK, thus :? (non-capturing parentheses). I want this to apply to LINK and also to <a href="LINK">LINK</a> so I put those tag parts in optional parentheses ()?. What I was wondering is, if there is way to say to the regex engine, if it didn't match (<a href=")? it doesn't need to match (">)? and (<a\/>) as it will only appear when together.

 

I found conditional patterns (?(condition)yes-pattern|no-patter) but couldn't figure out how to use them and if it's even possible to do.

 

Any help much appreciated.

Link to comment
Share on other sites

It was a thing for friend, he wanted to replace all e-mails, e.g.:

 

email@email.com
mailto:email@email.com
<a href="mailto:email@email.com">email@email.com</a>

 

with his <script> that would document.write the mail - as an anti-spam feature (although this solution is terrible).

 

So I created this function, to accomodate for all three possibilites (I know it has flaws):

 

<?php
#
function removeMails($text){   
#
        $pattern = '_(?:<a href=")?(?:mailto:)?([-[:alnum:]]+?)@([-[:alnum:]]+?)\.([-[:alnum:]]{2,}?)(?:">[^"\']+</a>)?_i';
#
        $replace = "<script type='text/javascript'>spam_email('%s', '@', '%s', '%s');</script>";
#
        $sanitize = create_function('$matches', 'return sprintf("<script type=\'text/javascript\'>spam_email(\'%s\', \'@\', \'%s\', \'%s\');</script>", $matches[3], $matches[2], $matches[1]);');
#
        return preg_replace_callback($pattern, $sanitize, $text);
#
}

 

Only thing I'm curious about is whether I can put a conditional pattern in there as I said above.

 

P.S.: I know the e-mail pattern is lame, I'm going to replace it with better one. Not the RFC compliant, though (lol).

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.