The Little Guy Posted May 20, 2008 Share Posted May 20, 2008 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? Quote Link to comment Share on other sites More sharing options...
smc Posted May 20, 2008 Share Posted May 20, 2008 <?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. Quote Link to comment Share on other sites More sharing options...
rhodesa Posted May 20, 2008 Share Posted May 20, 2008 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)); ?> Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted May 20, 2008 Author Share Posted May 20, 2008 @smc doing that will only use highlight string as a string not a function. Quote Link to comment Share on other sites More sharing options...
rhodesa Posted May 20, 2008 Share Posted May 20, 2008 the e modifier on regex will have it execute the replacement as PHP...i tested it and it looks good Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted May 20, 2008 Author Share Posted May 20, 2008 PERFECT! Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted May 20, 2008 Author Share Posted May 20, 2008 Do you know of a way for me to have the question bold with paragraph tags? Quote Link to comment Share on other sites More sharing options...
rhodesa Posted May 20, 2008 Share Posted May 20, 2008 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> Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted May 20, 2008 Author Share Posted May 20, 2008 OMG, I was trying to be too complex with the css Thanks you way works! Quote Link to comment Share on other sites More sharing options...
Orio Posted May 20, 2008 Share Posted May 20, 2008 You should also add the "i" modifier for opening tags like <?PHP. Orio. Quote Link to comment Share on other sites More sharing options...
rhodesa Posted May 20, 2008 Share Posted May 20, 2008 You should also add the "i" modifier for opening tags like <?PHP. Orio. good call on the i Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.