how to display an array content inside CKeditor? - php

I need to display the content of doc file inside CKeditor.
I read the content of doc file & passing it into an array line by line :
$rs = fopen("text.doc", "r");
while ($line = fgets($rs, 1024)) {
$this->data[] = $line . "<BR>";
}
then I create an instance of CKeditor:
include_once("ckeditor/ckeditor.php");
$CKeditor = new CKeditor();
$CKeditor->basePath = '/ckeditor/';
foreach ($this->data as $value) {
//what should I write here
}
$CKeditor->editor('editor1');
the CKeditor work right now & appear on my webpage .. but without any content ?
what should I right inside the foreach to passing array content into the editor ?
please help =(

.doc files are zipped up and cannot be read like this, by line. Consider using PHPWord to get access to the contents inside.
EDIT: Looks like PHPDoc can only write and not read, upon further investigation.
PHP tools are very deficient in this area. Your best bet is to use something like DocVert to do your file conversions on the command line. THEN you could load that document inside CKEditor.
EDIT: after OP's comment:
let's consider it's a txt file ... I need the Ckeditor method
Load your decoded HTML content into a Textarea, and give this textarea an HTML ID or class:
$textarea_content = htmlspecialchars_decode(file_get_contents('text.doc'));
Then, in your HTML, call the CKEditor inside a JavaScript tag to replace the textarea with the editor:
<html>
<head>
<!-- include CKEditor in a <script> tag first -->
<script type="text/javascript">
window.onload = function()
{
CKEDITOR.replace( 'editor1' );
};
</script>
</head>
<body>
<textarea id="editor1" name="editor1"><?php echo $textarea_content ?></textarea>
</body>
The documentation page has a lot more details.

Related

How to display data in ckeditor from mysql in PHP

I am new in this field, I used ckeditor but I don't know how to display data in ckeditor through mysql..
My code is
$CKeditor = new CKeditor();
$CKeditor->BasePath = 'manage-site/';
$CKeditor->config['filebrowserBrowseUrl'] = 'ckfinder/ckfinder.html';
$CKeditor->config['filebrowserImageBrowseUrl'] = 'browser/browser.php?type=Images';
$CKeditor->config['filebrowserFlashBrowseUrl'] = 'ckfinder/ckfinder.html?type=Flash';
$CKeditor->config['filebrowserUploadUrl'] = 'ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Files';
$CKeditor->config['filebrowserImageUploadUrl'] = 'uploader/uploader.php?type=Images';
$CKeditor->config['filebrowserFlashUploadUrl'] = 'ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Flash';
$CKeditor->editor('page_desc','**page_desc**');
For display content I use
<?php echo stripslashes($details[0]["page_desc"]); ?>
I wanted to display on the place of Page_desc(last Line of Code). How to do that?
You can simply download the ckeditor package and do this simple 2 step
Step 1 : Include the ckeditor.js from your file
<script type="text/javascript" src="ckeditor/ckeditor.js"></script>
Step 2 : Include the class name as ckeditor to your textarea
Then you can see the ckeditor with simple step
Note : Make sure the ckeditor.js is in the proper path
Hope this helps you

how to strip php tags from textarea?

I am trying to remove tags from a textarea.
I open a php page in a textarea successfully using the code bellow but I only want the code to be there and remove the <?php from the top of the page and ?> from the bottom of the page in the textarea.
this is my php code for loading the php page:
<?php
$fn = "phpPage.php";
if (isset($_POST['content']))
{
$content = stripslashes($_POST['content']);
$fp = fopen($fn,"w") or die ("Error opening file in write mode!");
fputs($fp,$content);
fclose($fp) or die ("Error closing file!");
}
?>
and this is how i load the page in the textarea:
<textarea id="code" style="width:450px;" rows="25" cols="50" name="content"><?php readfile($fn); ?></textarea>
I tried this and it didn't work without giving me any error:
strip_tags($fn, '<?php ?>');
could someone please advise on this?
Thanks
EDIT: I've tried all the suggestions and none worked so far.
see this for an inspiration
$string = "SO.ME.TE.XT";
$string = str_replace(".","",$string);
echo($string);
will return "SOMETEXT". Hence
$string = YOUR PHP CODE;
$string = str_replace("<?php","",$string);
$string = str_replace("?>","",$string);
echo($string);
Should work in your case.
Update 1:
Another approach would be using jquery and a simple replace script:
$(document).ready(function () {
var codeContents = $("#code").val();
$("#code").val(codeContents.replace("<?php","").replace("?>",""));
});
Its not possible to do it in php.
There are ways to read content of a .php file:-
-> highlight_string(file_get_contents($fn));
-> readfile($fn);
-> $Vdata = file_get_contents($fn);
$abc = var_dump($Vdata);
But these can only read the content and also these functions are not returning the output, so that we can assign them to any variable and can use them as per our wish.
We can just print/display the content of a .php file..
Well, the Jquery code shared by "JKurcik" can work for you but the issue you are facing becuase you are using that code in your .php file***(NOTE: In a .php file whenever or wherever we put "<?php", it starts considering the start of php code)*.
You can make this working by adding that code
$(document).ready(function () {
var codeContents = $("#code").val();
$("#code").val(codeContents.replace("<?php","").replace("?>",""));
});
in a seperate custom.js file and call that js file in your .php doc, then it'll work for sure.

Save the contents of manipulated div to a variable and pass to php file

I have tried to use AJAX, but nothing I come up with seems to work correctly. I am creating a menu editor. I echo part of a file using php and manipulate it using javascript/jquery/ajax (I found the code for that here: http://www.prodevtips.com/2010/03/07/jquery-drag-and-drop-to-sort-tree/). Now I need to get the edited contents of the div (which has an unordered list in it) I am echoing and save it to a variable so I can write it to the file again. I couldn't get that resource's code to work so I'm trying to come up with another solution.
If there is a code I can put into the $("#save").click(function(){ }); part of the javascript file, that would work, but the .post doesn't seem to want to work for me. If there is a way to initiate a php preg_match in an onclick, that would be the easiest.
Any help would be greatly appreciated.
The code to get the file contents.
<button id="save">Save</button>
<div id="printOut"></div>
<?php
$header = file_get_contents('../../../yardworks/content_pages/header.html');
preg_match('/<div id="nav">(.*?)<\/div>/si', $header, $list);
$tree = $list[0];
echo $tree;
?>
The code to process the new div and send to php file.
$("#save").click(function(){
$.post('edit-menu-process.php',
{tree: $('#nav').html()},
function(data){$("#printOut").html(data);}
);
});
Everything is working EXCEPT something about my encoding of the passed data is making it not read as html and just plaintext. How do I turn this back into html?
EDIT: I was able to get this to work correctly. I'll make an attempt to switch this over to DOMDocument.
$path = '../../../yardworks/content_pages/header.html';
$menu = htmlentities(stripslashes(utf8_encode($_POST['tree'])), ENT_QUOTES);
$menu = str_replace("<", "<", $menu);
$menu = str_replace(">", ">", $menu);
$divmenu = '<div id="nav">'.$menu.'</div>';
/* Search for div contents in $menu and save to variable */
preg_match('/<div id="nav">(.*?)<\/div>/si', $divmenu, $newmenu);
$savemenu = $newmenu[0];
/* Get file contents */
$header = file_get_contents($path);
/* Find placeholder div in user content and insert slider contents */
$final = preg_replace('/<div id="nav">(.*?)<\/div>/si', $savemenu, $header);
/* Save content to original file */
file_put_contents($path, $final);
?>
Menu has been saved.
To post the contents of a div with ajax:
$.post('/path/to/php', {
my_html: $('#my_div').html()
}, function(data) {
console.log(data);
});
If that's not what you need, then please post some code with your question. It is very vague.
Also, you mention preg_match and html in the same question. I see where this is going and I don't like it. You can't parse [X]HTML with regex. Use a parser instead. Like this: http://php.net/manual/en/class.domdocument.php

How to open a link which was parsed in a div?

My site parses a spanish dictionary and lets you search for more than one word at a time. If you look up "hola" you get the first div). Some words come up with suggestions, like "casa", instead of definitions like "hola":
And what i am looking for is this:
So, i would like to: when you click on the suggestions (like CASAR in the example I posted) to print the result in a div like HOLA. Here is the code used:
$words = array('word0','word-1');
function url_decode($string){
return urldecode(utf8_decode($string));
}
$baseUrl = 'http://lema.rae.es/drae/srv/search?val=';
$cssReplace = <<<EOT
<style type="text/css">
// I changed the style
</style>
</head>
EOT;
$resultIndex = 0;
foreach($words as $word) {
if(!isset($_REQUEST[$word]))
continue;
$contents = file_get_contents($baseUrl . urldecode(utf8_decode($_REQUEST[$word])));
$contents = str_replace('</head>', $cssReplace, $contents);
$contents = preg_replace('/(search?[\d\w]+)/','http://lema.rae.es/drae/srv/search', $contents);
echo "<div style='
//style
", (++$resultIndex) ,"'>", $contents,
"</div>";
}
I have tried with: $contents .= '' . $word . '<br/>'; But it didn't work nor I know really where/how to use it.
Okay, I'll use jQuery for the example because it will be the easiest to get the job done specially if you are new to programming.
NOTE: I DO NOT RECOMMEND USING JQUERY -BEFORE- LEARNING JAVASCRIPT -- DO IT AT YOUR OWN RISK, BUT AT LEAST COME BACK AND LEARN JAVASCRIPT LATER
First, read up on how to download and install jquery here.
Secondly, you will want something a little like this, let's pretend this is your markup.
<div id="wrapper">
<!-- This output div will contain the output of whatever the MAIN
word being displayed is, this is where HOLA would be from your first example -->
<div id="output">
</div>
<!-- This is your suggestions box, assuming all anchor tags in here will result in
a new word being displayed in output -->
<div id="suggestions">
</div>
</div>
<!-- Le javascript -->
<script>
// Standard jQuery stuff, read about it on the jquery documentation
$(function() {
// The below line is a selector, it will select any anchor tag inside the div with 'suggestions' as identifier
$('#suggestions a').click(function(e) {
// First, stop the link going anywhere
e.preventDefault();
// Secondly, we want to replace the content from output with new content, we will use AJAX here
// which you should also read about, basically we set a php page, and send a request to it
// and display the result without refreshing your page
$.ajax({
url: 'phppage.php',
data: { word: 'casar' },
success: function(output) {
// This code here will replace the contents of the output div with the output we brought back from your php page
$('#output').html(output);
}
})
});
})
</script>
Hopefully the comments will shed some light, you need to then set up your php script which will be sent a GET request. (for example, http://some.address.com/phppage.php/?word=casar)
Then you just echo out the output from PHP
<?php
$word = $_GET['word'];
// Connect to some database, get the definitions, and store the results
$result = someDatabaseFunctionThatDoesSomething($word);
echo $result;
?>
Hope this helps, I expect you have a lot of reading to do!

Select Content of div using php

I have a div named "main" in my page. I put the code to convert a html into pdf using php at the end of page. I want to select the content (div named main contains paragraphs, charts, tables etc.).
How ?
Below code will show you how to get DIV tag's content using PHP code.
PHP Code:
<?php
$content="test.html";
$source=new DOMdocument();
$source->loadHTMLFile($content);
$path=new DOMXpath($source);
$dom=$path->query("*/div[#id='test']");
if (!$dom==0) {
foreach ($dom as $dom) {
print "
The Type of the element is: ". $dom->nodeName. "
<b><pre><code>";
$getContent = $dom->childNodes;
foreach ($getContent as $attr) {
print $attr->nodeValue. "</code></pre></b>";
}
}
}
?>
We are getting DIV tag with ID "test", You can replace it with your desired one.
test.html
<div id="test">This is my content</div>
Output:
The Type of the element is: div
This is my content
You should put the php code into a separate file from the html and use something like DOMDocument to get the content from the div.
$dom = new DOMDocument();
$dom->loadHTMLFile('yourfile.html');
...
You cannot directly interact with the HTML DOM via PHP.
What you could do, is using a with an input containing your content. When submitting the form you can access the data via PHP.
But maybe you want to use Javascript for that task?
Nevertheless, a quick'n'dirty PHP example:
<form action="" method="post">
<textarea name="content">hello world</textarea>
</form>
<?php
if (isset($_POST['content'])) {
echo $_POST['content'];
}
?>

Categories