metazai Posted February 27, 2009 Share Posted February 27, 2009 Here's a fun one. I've got a CMS, and for editing purposes, I'm building in a script manager. It's like this -- the WYSIWYG editor (InnovaStudio) I'm using in the CMS doesn't like plug-in javascript, so I'm going to allow the user to maintain a little script library elsewhere in the management for their google tracking codes or whatever, and have a dropdown on the page editor when they want to plug one in. I've built a version of the inserter already and it works well. The problem is the JavaScript, so I'm going to let the inserter plug in a placeholder for the script, then when they submit changes to the page it will convert, for instance "<<<>>>google.script<<<>>>" into the chunk of JavaScript that needs to actually be written to the page, and write it into the page. Now, if they pull up that page again, it needs to find that script before it's loaded into the WYSIWYG and convert it back to a placeholder so it doesn't get screwed up by the WYSIWYG. Clear as mud, right? Well here's the catch. In the following code, I just run through my database table of scripts to see if any match (they are all saved with identical "<!-- start." and "<!-- end" pieces when the users enters them to begin with), and hopefully replace them with my fancy placeholder. Only it doesn't work, it grabs pieces of other tags and replaces them, multiple times. As always when I post here, I'm at my wit's end, which admittedly isn't very far: $scriptquery = "SELECT * FROM scripts"; $scriptresult = mysql_query($scriptquery); while ($scriptrow = mysql_fetch_array($scriptresult)) { $process_length_start=(strlen($scriptrow['name']) + 15); $process_length_end=(strlen($scriptrow['name']) + 12); $scriptstart = (strpos($currentcode, "<!-- start.".$scriptrow['name']." -->") - $process_length_start); $scriptend = (strpos($currentcode, "<!-- end.".$scriptrow['name']." -->") + $process_length_end); $scriptcode=substr($currentcode, $scriptstart, ($scriptend - $scriptstart)); $currentcode=ereg_replace($scriptcode,"<<<>>>".$scriptrow['name']."<<<>>>",$currentcode); } Any ideas? Quote Link to comment Share on other sites More sharing options...
RussellReal Posted February 27, 2009 Share Posted February 27, 2009 str_replace()? lol and have an array like $replace = array("<<<><><>>>google.script<<<><><>>>","<<<><><>>>woopra.script<<<><><>>>"); $with = array("<script src='google.js' type='text/javascript' />","<script src='woopra.js' type='text/javascript' />"); replace($replace,$with,$content); but this method is akward lol Quote Link to comment Share on other sites More sharing options...
metazai Posted February 27, 2009 Author Share Posted February 27, 2009 Thanks for your response, but you've got it backwards. In my code above, I'm searching for what you have as the $with string, to replace it with what you have as the $replace string. Still, a straight str_replace might do it, as I have the content of each script saved in the same database. Hmmm... Quote Link to comment Share on other sites More sharing options...
metazai Posted February 27, 2009 Author Share Posted February 27, 2009 $scriptquery = "SELECT * FROM scripts"; $scriptresult = mysql_query($scriptquery); while ($scriptrow = mysql_fetch_array($scriptresult)) { $replacewith="<<<>>>".$scriptrow['name']."<<<>>>"; $txtContent=str_replace($scriptrow['content'],$replacewith,$currentcode); } This code did it. Thanks for pointing me in the right direction! 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.