Jump to content

Replace Text


The Little Guy

Recommended Posts

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>

 

Link to comment
Share on other sites

<?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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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