jpratt Posted August 4, 2009 Share Posted August 4, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/168839-solved-exclude-script-tag/ Share on other sites More sharing options...
jpratt Posted August 5, 2009 Author Share Posted August 5, 2009 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>/' Quote Link to comment https://forums.phpfreaks.com/topic/168839-solved-exclude-script-tag/#findComment-891388 Share on other sites More sharing options...
Adam Posted August 5, 2009 Share Posted August 5, 2009 Give this a try: if (preg_match_all('#<script>(.*?)</script>#s', $str, $matches)) { print_r($matches); } Quote Link to comment https://forums.phpfreaks.com/topic/168839-solved-exclude-script-tag/#findComment-891413 Share on other sites More sharing options...
jpratt Posted August 5, 2009 Author Share Posted August 5, 2009 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; } } } Quote Link to comment https://forums.phpfreaks.com/topic/168839-solved-exclude-script-tag/#findComment-891433 Share on other sites More sharing options...
jpratt Posted August 5, 2009 Author Share Posted August 5, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/168839-solved-exclude-script-tag/#findComment-891442 Share on other sites More sharing options...
Adam Posted August 5, 2009 Share Posted August 5, 2009 Just use this... $content = preg_replace('#<script type="text/javascript">(.*?)</script>#s',"[*SCRIPT*]",$content); Quote Link to comment https://forums.phpfreaks.com/topic/168839-solved-exclude-script-tag/#findComment-891517 Share on other sites More sharing options...
jpratt Posted August 5, 2009 Author Share Posted August 5, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/168839-solved-exclude-script-tag/#findComment-891545 Share on other sites More sharing options...
nrg_alpha Posted August 6, 2009 Share Posted August 6, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/168839-solved-exclude-script-tag/#findComment-892193 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.