neoform Posted May 13, 2008 Share Posted May 13, 2008 I want to split this, but for some reason if i input this string: $str = '[[Cool Link|http://www.blah.com/]] [[hi there]]'; $str = preg_replace("`\[\[(.*)\|(.*)\]\]`siU", "<a href=\"\\2\">\\1</a>", $str); I get a messed up link that grabbed more than it should (I thought putting a U at the end makes it ungreedy.. Quote Link to comment Share on other sites More sharing options...
effigy Posted May 13, 2008 Share Posted May 13, 2008 It works for me: <a href="http://www.blah.com/">Cool Link</a> [[hi there]] Quote Link to comment Share on other sites More sharing options...
neoform Posted May 14, 2008 Author Share Posted May 14, 2008 I apparently oversimplified my problem.. $str = "[[url With Other Stuff in It|Link Title]] [[internal_link]] [[image: image_name.jpg|[[image Title and Link]]]]"; echo $str."<br />"; $str = preg_replace("`\[\[(?!Image:)(.*)\|(.*)\]\]`siU", "<a href=\"\\1\">\\2</a>", $str); $str = preg_replace("`\[\[(?!Image:)(.*)\]\]`siU", "<a href=\"\\1\">\\1</a>", $str); $str = preg_replace("`\[\[image:(.*)\]\]`siU", " ### \\1 @@@ ", $str); //to be completed echo $str; Quote Link to comment Share on other sites More sharing options...
effigy Posted May 14, 2008 Share Posted May 14, 2008 The pattern is ungreedy (lazy), but the engine always tries to make a match. .* does not indicate any stopping points, so the engine overshoots ]] to look for a pipe. Make this more specific, e.g.: `\[\[(?!Image:)([^\[\]]*)\|(.*)\]\]`siU P.S. Don't you want to use + instead of *? Quote Link to comment Share on other sites More sharing options...
neoform Posted May 14, 2008 Author Share Posted May 14, 2008 Ahh, cool. Yeah, + probably would be better. (I'm still learning regex's, I'm not always sure what's the appropriate control chars) Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.