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
https://forums.phpfreaks.com/topic/168839-solved-exclude-script-tag/
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>/'

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;
		}
	}
}

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.

Archived

This topic is now archived and is closed to further replies.

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