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? Link to comment https://forums.phpfreaks.com/topic/106496-solved-highlight_string/ 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. Link to comment https://forums.phpfreaks.com/topic/106496-solved-highlight_string/#findComment-545866 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)); ?> Link to comment https://forums.phpfreaks.com/topic/106496-solved-highlight_string/#findComment-545875 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. Link to comment https://forums.phpfreaks.com/topic/106496-solved-highlight_string/#findComment-545876 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 Link to comment https://forums.phpfreaks.com/topic/106496-solved-highlight_string/#findComment-545877 Share on other sites More sharing options...
The Little Guy Posted May 20, 2008 Author Share Posted May 20, 2008 PERFECT! Link to comment https://forums.phpfreaks.com/topic/106496-solved-highlight_string/#findComment-545885 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? Link to comment https://forums.phpfreaks.com/topic/106496-solved-highlight_string/#findComment-545887 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> Link to comment https://forums.phpfreaks.com/topic/106496-solved-highlight_string/#findComment-545895 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! Link to comment https://forums.phpfreaks.com/topic/106496-solved-highlight_string/#findComment-545900 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. Link to comment https://forums.phpfreaks.com/topic/106496-solved-highlight_string/#findComment-545904 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 Link to comment https://forums.phpfreaks.com/topic/106496-solved-highlight_string/#findComment-545909 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.