The Little Guy Posted April 7, 2008 Share Posted April 7, 2008 How can I take this: [ex=1]abs1 = abs(-4.2); // abs = 4.2; (double/float)[/ex] [ex=2]abs2 = abs(5); // abs2 = 5; (integer)[/ex] [ex=3]abs3 = abs(-5); // abs3 = 5; (integer)[/ex] and format it so it looks like this: <span class="exampleNumber">Example: 1</span><span class="example">abs1 = abs(-4.2); // abs = 4.2; (double/float)</span> <span class="exampleNumber">Example: 2</span><span class="example">abs2 = abs(5); // abs2 = 5; (integer)</span> <span class="exampleNumber">Example: 3</span><span class="example">abs3 = abs(-5); // abs3 = 5; (integer)</span> Quote Link to comment Share on other sites More sharing options...
Orio Posted April 7, 2008 Share Posted April 7, 2008 <?php $str = <<<DATA [ex=1]abs1 = abs(-4.2); // abs = 4.2; (double/float)[/ex] [ex=2]abs2 = abs(5); // abs2 = 5; (integer)[/ex] [ex=3]abs3 = abs(-5); // abs3 = 5; (integer)[/ex] DATA; $pattern = "/\[ex=([0-9]+)\](.*?)\[\/ex\]/"; $replacement = "<span class=\"exampleNumber\">Example: $1</span><span class=\"example\">$2</span>"; echo preg_replace($pattern, $replacement, $str); ?> Orio. Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted April 7, 2008 Author Share Posted April 7, 2008 PERFECT!!! I thought that is basically what I had... why didn't my version work? This is what I had: $pattern = "~\[ex=(.*)\](.*?)\[\/ex\]~"; Quote Link to comment Share on other sites More sharing options...
effigy Posted April 7, 2008 Share Posted April 7, 2008 That works, but it's not ideal. The use of . with a greedy quantifier creates a lot of backtracking. Orio's pattern works well by being more specific and applying laziness where necessary. Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted April 7, 2008 Author Share Posted April 7, 2008 Why doesn't this format? [ex=1]myFile = background_add("C:\Game_Maker7\example\images\GWarArrows_10.png", true,true,false))) fileString = string(myFile) show_message(fileString) // Returns interger[/ex] when I run it though this function: <?php bbcode($string){ return preg_replace('~\[ex=([0-9]+)\](.*?)\[\/ex\]~','<span class="exampleNumber">Example: $1</span><span class="example">$2</span>'$string); } ?> this is the output I get: [ex=1]myFile = background_add("C:\Game_Maker7\example\images\GWarArrows_10.png", true,true,false))) fileString = string(myFile) show_message(fileString) // Returns interger[/ex] Here is the page (Examples section): http://gmm.phpsnips.com/functions.php?function=background_add Here is what I would like it to look like (Examples section): http://gmm.phpsnips.com/functions.php?function=arctan2 it works on the second one why not this one? Quote Link to comment Share on other sites More sharing options...
Orio Posted April 8, 2008 Share Posted April 8, 2008 Oh, in the examples you gave each [ex][/ex] was in a single line. So you need to add the "s" modifier: <?php $str = <<<DATA [ex=1]abs1 = abs(-4.2); Another line // abs = 4.2; (double/float)[/ex] [ex=2]abs2 = abs(5); // abs2 = 5; (integer)[/ex] [ex=3]abs3 = abs(-5); // abs3 = 5; (integer)[/ex] DATA; $pattern = "/\[ex=([0-9]+)\](.*?)\[\/ex\]/s"; $replacement = "<span class=\"exampleNumber\">Example: $1</span><span class=\"example\">$2</span>"; echo preg_replace($pattern, $replacement, $str); ?> Orio. 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.