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
https://forums.phpfreaks.com/topic/99932-replace-text/
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
https://forums.phpfreaks.com/topic/99932-replace-text/#findComment-511095
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
https://forums.phpfreaks.com/topic/99932-replace-text/#findComment-511607
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
https://forums.phpfreaks.com/topic/99932-replace-text/#findComment-511962
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.