Jump to content

[SOLVED] highlight_string();


The Little Guy

Recommended Posts

in my database, I have a question, and sometimes the question has code with it.

 

it would look something like this in the database for example:

How would you replace a letter at a given position in a string?

 

<?php

    $a = 'fat';

?>

 

I would like to take that PHP, and place highlight_string(); around just the php portion, and not the question portion.

 

Anyone know the best way to do this?

Link to comment
Share on other sites

<?php

str_replace( "<?php", "highlight_string(", $questionFromDB );
str_replace( "?>", ");", $questionFromDB );

?>

 

If that doesn't work then do the get position in a string function (escapes me at the moment) and then isolate the section from that point to the end point.

Link to comment
Share on other sites

Try this on for size:

 

<?php
  $text = 'How would you replace a letter at a given position in a string?

<?php
     $a = \'fat\';
?>';
  print nl2br(preg_replace('/(\<\?php.+?\?>)/se','highlight_string(\'\\1\',1)',$text));
?>

Link to comment
Share on other sites

Add some style:

 

<style type="text/css">
  .question {
    font-weight: bold;
  }
  .question code {
    font-weight: normal;
  }
</style>
<p class="question"><?php print nl2br(preg_replace('/(\<\?php.+?\?>)/se','highlight_string(\'\\1\',1)',$text));?></p>

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.