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.. Link to comment https://forums.phpfreaks.com/topic/105492-titleurl/ 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]] Link to comment https://forums.phpfreaks.com/topic/105492-titleurl/#findComment-540313 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; Link to comment https://forums.phpfreaks.com/topic/105492-titleurl/#findComment-540633 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 *? Link to comment https://forums.phpfreaks.com/topic/105492-titleurl/#findComment-540914 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) Link to comment https://forums.phpfreaks.com/topic/105492-titleurl/#findComment-540985 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.