pandyboy Posted January 25, 2010 Share Posted January 25, 2010 Hi - I have (for example) the following in a field in my database... [[#201001251351#]]Some text here[[end]] Something else in this bit [[#201001251353#]]Some different text here[[end]] And more different bits [[#201001251357#]]Some more here[[end]] Further different bits which may include other replaced things I need it to display as the following: <a href="javascript:;" onclick="Show_Stuff('201001251351');\">Show</a> <div style="display:none;" id="201001251351">Some text here</div><br /> Something else in this bit <a href="javascript:;" onclick="Show_Stuff('201001251353');\">Show</a> <div style="display:none;" id="201001251353">Some different text here</div><br /> And more different bits <a href="javascript:;" onclick="Show_Stuff('201001251357');\">Show</a> <div style="display:none;" id="201001251357">Some more text here</div><br /> Further different bits which may include other replaced things This would be easy if I only needed to use the time value once in each replacement, but as you will see I use it twice each time and I can't figure out how to duplicate it. So in a nutshell I need to extract the bit between the [[# and #]] as a variable, then add it into both the javascript variable in the a tag and the id of the div tag and put everything between the #]] and the[[end]] in between the 2 div tags. Hope this makes sense! Any help much appreciated. I will probably kick myself later, as it'll no doubt be a blatant solution! Quote Link to comment https://forums.phpfreaks.com/topic/189725-using-preg_replace-to-replace-contents-into-2-locations/ Share on other sites More sharing options...
pandyboy Posted January 25, 2010 Author Share Posted January 25, 2010 OK, I think I am half way there now. I have adapted an example of preg_replace_callback from the PHP website, which gets me the value i need in 2 places, but I am having to use normal str_replace to finish the job, which seems a messy way to do it, as follows: $input = "[[#201001251351#]]Some text here[[end]] Something else in this bit [[#201001251353#]]Some different text here[[end]] And more different bits [[#201001251357#]]Some more here[[end]] Further different bits which may include other replaced things"; function parseTagsRecursive($input) { $regex = '#\[\[\#((?:[^[]|(?!/?\#]])|(?R))+)\#]]#'; if (is_array($input)) { $input = "<a href=\"javascript:;\" onclick=\"Show_Stuff('".$input[1]."');\">Vars</a> <div style=\"display:none;\" id=\"".$input[1]."\">"; } return preg_replace_callback($regex, 'parseTagsRecursive', $input); } $output = parseTagsRecursive($input); echo str_replace("[[end]]","</div>",$output); This gives the desired output, but can anyone improve on this? Quote Link to comment https://forums.phpfreaks.com/topic/189725-using-preg_replace-to-replace-contents-into-2-locations/#findComment-1001313 Share on other sites More sharing options...
thebadbad Posted January 25, 2010 Share Posted January 25, 2010 I would just go with a simple <?php $str = '[[#201001251351#]]Some text here[[end]] Something else in this bit [[#201001251353#]]Some different text here[[end]] And more different bits [[#201001251357#]]Some more here[[end]] Further different bits which may include other replaced things'; $replace = '<a href="javascript:;" onclick="Show_Stuff(\'$1\');">Show</a> <div style="display:none;" id="$1">$2</div><br />'; echo preg_replace('~\[\[#([0-9]+)#\]\](.*?)\[\[end\]\]~is', $replace, $str); ?> Quote Link to comment https://forums.phpfreaks.com/topic/189725-using-preg_replace-to-replace-contents-into-2-locations/#findComment-1001520 Share on other sites More sharing options...
pandyboy Posted January 26, 2010 Author Share Posted January 26, 2010 Thanks - much better! I really must get my head round regex!! If I wanted to add in 2 more replacements, for example replace [* & *] with <b> & </b> and [~ & ~] with <i> & </i>, could I do these with the same line of code, or would it require a subsequent preg_replace? Quote Link to comment https://forums.phpfreaks.com/topic/189725-using-preg_replace-to-replace-contents-into-2-locations/#findComment-1001840 Share on other sites More sharing options...
thebadbad Posted January 27, 2010 Share Posted January 27, 2010 No worries. In that case you can define an array of patterns and replacements: $replace = array( '~\[\[#([0-9]+)#\]\](.*?)\[\[end\]\]~is' => '<a href="javascript:;" onclick="Show_Stuff(\'$1\');">Show</a> <div style="display:none;" id="$1">$2</div><br />', '~\[\*(.*?)\*\]~s' => '<strong>$1</strong>', '#\[~(.*?)~\]#s' => '<em>$1</em>' ); $str = preg_replace(array_keys($replace), $replace, $str); I used strong and em tags since the b and i tags are deprecated. Quote Link to comment https://forums.phpfreaks.com/topic/189725-using-preg_replace-to-replace-contents-into-2-locations/#findComment-1002387 Share on other sites More sharing options...
pandyboy Posted February 8, 2010 Author Share Posted February 8, 2010 Thanks - that works beautifully. (By the way, B & I aren't deprecated. The difference between B & I and STRONG & EM is that the latter are used by screen readers to add strength or emphasis to words. As what I am doing is not ever intended for screen readers, but is purely visual, B & I are more appropriate) Quote Link to comment https://forums.phpfreaks.com/topic/189725-using-preg_replace-to-replace-contents-into-2-locations/#findComment-1008759 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.