0

I'm doing a regular expression for a linking system, and the syntax looks like this:

<a href=":login">Login</a>

This tells the system that this link should be converted to JS or an HTML destination depending on the user's browser capabilities.

Right, so I have all the back-end stuff working fine, but I noticed a strange problem with the regular expresion that I'm using to catch these types of links. When a dynamic link (href=":) stands by itself (i.e. not next to another object) then it works fine; however, if a dynamic link like

<a href=":myLink">

comes after a standard link like

<a href="myLink">

then the dynamic link doesn't get altered, like it should.

Here is a codepad link to some sample code that demonstrates the bug. http://codepad.org/ZKdm2NkS

Notice the <a href=":first"> link does not get modified but the <a href=":second"> link does.

I'm not very good with regexps so I'm sure there's a better way of handling things rather than just using a (.*) everywhere you turn, but like I said, I'm open to better ideas and opinions.

andrewsi
  • 10,807
  • 132
  • 35
  • 51
SISYN
  • 2,209
  • 5
  • 24
  • 45

1 Answers1

1

since the only thing you are replacing is the ":myLink" portion you don't really need to match the rest... try this:

$html = preg_replace('/href=":([\w]+)"/', 'href="processedLink-$1"', $html);

this is matching only word (\w) characters (letters, digits, underscores)

dshu610
  • 191
  • 3