Jump to content

[SOLVED] exclude script tag


jpratt

Recommended Posts

OK. I have this in another thread, but this is so specific and off the topic of the other thread I need to concentrate on this.

 

I am placing content into an html structure. I have values in my database that have html stored. I am removing the content from the html string and placing placeholders to be filled with new content in another language. The only problem I have is in the content there is a script tag that is getting removed. This in turn creates an extra placeholder so the text ends up being placed in the wrong areas. The question is how do I exclude this one placeholder from being created? Here is the code for both creating the placeholder and placing content back into the string.

 

// $content represents original content html string
$content = $row['content'];
// remove content and put in placeholders where the translated text is to go
$content = preg_replace('~(>)(?!\s*<).*?(<|$)~s',"$1[*CONTENT*]$2",$content);

//open file from translator and place numbered lines in array
$file = "translated.rtf";
$fh = fopen($file, 'r');
$data = fread($fh, filesize($file));
fclose($fh);
$array = explode("\n", $data);

//title is in separate field, place first translated line in projects in the title field
$x = 1;
foreach($array as $a) {
	if($x == 1) {
		$title = substr($a, 4);
	} else {
		$a = substr($a, 4);
		$content = preg_replace('~\[\*CONTENT\*\]~',$a,$content,1);

	}
	$x++;
}
echo $content

Link to comment
Share on other sites

Looking at it, I am going to do a replace on all the content between the script tags to create a second set of placeholders. But first I need to store the content from between the tags in an array. How do you get everything from between two tags and store in a variable? I looked over everything on php.net but most are replace or match. Is there a way of giving it and expression and have it return the content that matched it? I think this is my expression:

 

'/<\script>(.*?)<\/script>/'

Link to comment
Share on other sites

Thanks, got it working. Here is the code for getting the script content into a simple array:

 

if(preg_match_all('#<script type="text/javascript">(.*?)</script>#s', $content, $matches)) {
	$scriptarr = array();
	for ($row = 1; $row < 2; $row++) {
		foreach($matches[$row] as $field) {
			$scriptarr[] = $field;
		}
	}
}

Link to comment
Share on other sites

Now I tried this to replace the content with a placeholder but removes the script tags all together without making placeholders:

 

$content = preg_replace('#<script type="text/javascript">(.*?)</script>#s',"$1[*SCRIPT*]$2",$content);

 

Any ideas?

Link to comment
Share on other sites

This throws an error:

 

Warning: preg_replace() [function.preg-replace]: Unknown modifier 'j' in C:\Program Files\reporting\Program\www\localhost\trans\import.php on line 20

 

Are you using / as delimiters? Usually that kind of error arises when the character of choice for the delimiter is also found (unescaped) within the pattern. As a result, the regex engine expects this to be the end of the pattern, thus expects the characters that follow to be legal modifiers.

 

Since the j character is what your system is balking at (I am guessing at this point: text/javascript ?), the only thing that comes to mind is the use of / characters as your delimiters... if so, either escape the inner / characters inside the pattern, or use a different delimiter character.

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.