Jump to content

[[title|url]]


neoform

Recommended Posts

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

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.