Jump to content

[SOLVED] search and replace a string within a string


metazai

Recommended Posts

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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...

Link to comment
Share on other sites

$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!

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.