Jump to content

Help: preg_replace


clown[NOR]

Recommended Posts

I asked this a while ago, but couldn't get it to work, so I just gave up for a while.. but i'm back :D what I'm trying to do is to make so that I can enter a php code and it apears like it does here when we use the [ code] [/ code]... I found a code that should do it, but it's not working as I want it to. I've tried modifying the pattern, but since I'm not that good with preg_replace it most of the time results in error messages.

 

the code I'm using is:

<?php

if (isset($_REQUEST['Submit'])) {

	$s = $_REQUEST['text'];
	$s = str_replace("]\n", "]", $s);
	$match = '#\[code\](.*)\[\/code\]#se';
	$replace = "'<div>'.highlight_string(stripslashes('$1'), true).'</div>'";
	echo preg_replace($match, $replace, $s);

}

?>
<form name="code" method="post" action="code.php">
<textarea name="text" cols="100" rows="40"></textarea>
<br><input type="submit" name="Submit" value="Submit">
</form>

 

Then I try to use this code in the textarea:

<?php

if (isset($_REQUEST['Submit'])) {

	$s = $_REQUEST['text'];
	$s = str_replace("]\n", "]", $s);
	$match = '#\[code\](.*)\[\/code\]#se';
	$replace = "'<div>'.highlight_string(stripslashes('$1'), true).'</div>'";
	echo preg_replace($match, $replace, $s);

}

?>

 

But the result ends up like this:

<?php 

    if (isset($_REQUEST['Submit'])) { 
     
        $string = str_replace("]\n\", \"]\", $_REQUEST['text']); 
        $string = str_replace(\"\\\", \"\", $string); 
         
        $text = preg_replace(\"#\[code\](.*?)\[\/code\]#se\", \"'<div>'.highlight_string(stripslashes('$1'), true).'</div>'\", $string); 
         
        echo $text; 
     
    } 

?>

 

any ideas on how to solve this one?

 

Thanks In Advance

- Clown

Link to comment
Share on other sites

I had a slash issue with the /e modifier awhile ago while doing something similar.  It tends to add slashes where they're not wanted.  Try this:

 

<?php
if (isset($_REQUEST['Submit'])) {
$_REQUEST['text'] = stripslashes($_POST['text']);
$match = array(
	"/\]\n/",
	'#\[code\](.*)\[/code\]#se',
	'/\\\\"/');
$replace = array(
	']',
	'"<div>".highlight_string(\'$1\',true)."</div>"',
	'"');
echo preg_replace($match, $replace, $_REQUEST['text']);
}
?>

Link to comment
Share on other sites

awsome.. that solved the slash problem, thanks.. but there's one issue left.. how can I stop the code from become red from one point and all the way to the end?

 

like in the last code view in my first post

 

EDIT: i also wonder what the se represents in the pattern

Link to comment
Share on other sites

link=topic=138998.msg589992#msg589992 date=1178214150]

EDIT: i also wonder what the se represents in the pattern

 

That was in your original code.  They're modifiers.  'e' means treat the replacement like code and use the output of that code as the replacement text.  's' means the dot (.) matches newlines.

 

I'm not sure about the red code, though; it's probably a parsing issue related to quotes.  My prefered editor (Crimson Editor) sometimes gets its syntax coloring wrong when it comes to quotes in PHP.  Maybe highlight_string() has similar difficulties.

Link to comment
Share on other sites

oh ok..thanks for the quick answer... do you know anywhere I can read about modifiers? tried lokoing for it on php.net and the manual, but couldn't see anything.. I'm gonna go trough it again...

 

about the red code, thanks for the info... anyone know how to solve the problem?

 

EDIT: Found info about modifiers...

Link to comment
Share on other sites

$match = array(
	"/\]\n/",
	'#\[code\](.*)\[/code\]#se',
	'/\\\\"/');

 

Matches

]

Grabs everything here

\\"

 

replaces

]

"<div><font color="#0000BB">Grabs everything here</font></div>"

 

 

of course "Grabs everything here" is formatted with <font color="#0000BB"></font> using the highlight_string

 

 

can you post some example text.. :P

Link to comment
Share on other sites

Sorry PC started to reboot so hit post

 

try here

 

$theText = "here is some [ code]sample data[ /code] is this ok";//<--remove spaces
$result = preg_replace('~\\[code\\](.*)\\[\\\/code\\]~sim', "highlight_string('$1')", $theText);
echo $result;

Link to comment
Share on other sites

$result = preg_replace('/\[code\](.*)\[\/code\]/')~sim', "highlight_string('$1')", $theText);

 

That should do the trick.... Not sure why MadTechie had all those extra slashes....

 

Maybe I missed something >.< lol

Link to comment
Share on other sites

sorry mate.. didn't work...

 

returned the followin:

Warning: preg_replace() [function.preg-replace]: Unknown modifier ')' in G:\www\test\code.php on line 16

 

nevermind: I removed the ) at the end, but the it gave me unknown modifier ~...  must be something else

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.