Jump to content

preg_match_all issue?


adamf

Recommended Posts

Hi,

 

I am writing a bit of code so that I can place php inside a content management system that I have developed by using tags like:

 

[code=php:0]echo 'red';

[/code]

 

The code above works but if I enter the code as seen below then it prints what you see on the page and doesn't treat it as php code...i'm very confused?!:

 

[code=php:0]
echo 'red';

[/code]

 

This is the code that I am using...

 

preg_match_all('/\[code=php:0](.*?)\[\/php]/', $content, $data, PREG_SET_ORDER); 

$find[] = "";
$replace[] = "";

foreach ($data as $info) 

{

$main = $info[1];	

		ob_start();
	eval("$main");
	$f = ob_get_contents();
	ob_end_clean(); 


	$find[] = $info[0];
	$replace[] = $f;
}	

$content = str_replace($find, $replace, $content);
return $content;

 

If you could point out what the issue is I would be very greatful :)

 

Regards,

Adam

Link to comment
Share on other sites

I've got a similar problem with a bbcode system I'm making. It's something to do with the newline character.

I haven't fixed it yet, but it was quite late last night I was working on it.

 

I tried converting newlines to <br /> in hopes that it would work, but didn't seem to:

preg_match_all('\[code=php:0](.*?)\[\/php]', $content, nl2br($data), PREG_SET_ORDER); 

Link to comment
Share on other sites

If I have:

 

[code=php:0]

echo 'Display this text';

echo 'And this text';

[/code]

 

Can anything other than preg_match_all() be used to return the code below as lets say $value ?

 

echo 'Display this text';

echo 'And this text';

 

I can then run $value through eval() to have it display as I need...

 

I just don't understand how if everything is on one line it works but as soon as a break is made it doesn't?

 

 

Link to comment
Share on other sites

Thank you for the advice but i'm not quite sure what you mean?

 

If I have:

 

preg_match_all('/\[code=php:0](.*?)\[\/php]/', $content, $data, PREG_SET_ORDER);

 

How would I use the /s modifier to make . match new lines?

 

Thank you for your patience :)

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.