Solved

Hyperlinks not formatting correctly using TinyMCE

Posted on 2009-05-03
5
484 Views
Last Modified: 2013-12-13
Hi, I am trying to insert hyperlinks on my pages using TinyMCE.

I have tried both using the toolbar option and adding the link details in there, and also manually, but each time it is outputting wrong.

instead of the link being http://www.numyspace.co.uk/~unn_r031923/Testing/login-OOtest.php

it is being outputted like this.

http://www.numyspace.co.uk/~unn_r031923/Testing/\"login-OOtest.php\"

I have searched the help files and found this below. The question is, how could i adapt this script into my index.php code?



HTML output include lots of \" like <a href=\"mylink.htm\">link</a>?

This is probably because you are using PHP and it has a feature that's called magic quotes that is enabled by default. You can read more about this in the PHP documentation.

A quick way to clean up magic quotes is to use this (taken from the PHP manual):

if (get_magic_quotes_gpc()) {
    function stripslashes_deep($value)
    {
        $value = is_array($value) ?
                    array_map('stripslashes_deep', $value) :
                    stripslashes($value);

        return $value;
    }

    $_POST = array_map('stripslashes_deep', $_POST);
    $_GET = array_map('stripslashes_deep', $_GET);
    $_COOKIE = array_map('stripslashes_deep', $_COOKIE);
    $_REQUEST = array_map('stripslashes_deep', $_REQUEST);
}


index.php

-----------------

<?php

include("header.html");
 

// Get the page name from the query string

$page = $_GET['page'] .'.txt';

if (!$_GET['page']) {
 

  echo("<h1>Page name not specified</h1>\n");

  include("../footer.html");

  exit;

}
 

// This step is checking to see if I have posted. 

// If I have posted the page contents, it opens up the 

// filename indicated by the query string, writes it out, 

// closes the file, shows  the new contents of the file, 

// sources the footer and quits.

if (isset($_POST['page'])) {

  $handle = fopen("../pages/$page", 'w');

  fwrite($handle, $_POST['page']);

  fclose($handle);

  include("../footer.html");

  exit;

}
 

// If I have specified a page that exists, read it into the 

// $text variable. If the page doesn't exist, 

// mention that I am creating a new one at the top of the page, 

// and set $text to an empty paragraph (<p></p>). 

if (file_exists("../pages/$page")) {

  $FILE = fopen("../pages/$page", "rt");

  while (!feof($FILE)) {

    $text .= fgets($FILE);

  }

  fclose($FILE);

} else {

   echo("<h1>New Page: $page</h1>\n");

  $text = "<p></p>";

}
 
 

// JavaScript is usually loaded in the <head> section of a page. 

// But since the <head> section is // in the shared header.html, 

// I don't want to load this up for every page, just the editing page. 

// Because of this, i am putting it in the <body> section. 

// The "init" section is where all the // options are for the editor. 

// "mode: textareas" means that all text areas on the page will become

// TinyMCE WYSIWYG texareas. 

echo <<< EOM

<script language="javascript" type="text/javascript" src="/~unn_r031923/Testing/tinymce/jscripts/tiny_mce/tiny_mce.js"></script>

<script language="javascript" type="text/javascript">

tinyMCE.init({
 

// General options

mode : "textareas",

theme : "advanced",
 

plugins : "safari,spellchecker,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,imagemanager,filemanager",
 

// Theme options

theme_advanced_buttons1 : "save,newdocument,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,styleselect,formatselect,fontselect,fontsizeselect",
 

theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,help,code,|,insertdate,inserttime,preview,|,forecolor,backcolor",
 

theme_advanced_buttons3 : "tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,emotions,iespell,media,advhr,|,print,|,ltr,rtl,|",
 

theme_advanced_buttons4 : "moveforward,movebackward,absolute,|,styleprops,spellchecker,|,cite,abbr,acronym,del,ins,attribs,|,visualchars,nonbreaking,template,blockquote,|,insertfile,insertimage",
 

theme_advanced_toolbar_location : "top",

theme_advanced_toolbar_align : "left",

theme_advanced_resizing : true,
 

// Example content CSS (should be your site CSS)

content_css : "Testing/project.css",
 

// Drop lists for link/image/media/template dialogs

template_external_list_url : "js/template_list.js",

external_link_list_url : "js/link_list.js",

external_image_list_url : "js/image_list.js",

media_external_list_url : "js/media_list.js",
 
 

});

</script>

EOM;
 

// Here I create the form, the textarea and the "Save" button. 

// The "htmlspecialchars($text)" call reads the $text variable,

// and replaces all the special characters with the 

// HTML special character codes. This is so the HTML in 

// the file doesn't clash with the HTML on this page.

// The output of this function then gets passed to the textarea.

// When the "Save" is clicked, it calls the same page again 

// with the same query string, but this time there is some posted

// information containing the editing i did. 

// This is sent to the second section of code at the top

// of this page to be saved to a file.
 

$this_page = $_SERVER['PHP_SELF'];

$query_string = $_SERVER['QUERY_STRING'];

echo("<form method=\"post\" action=\"$this_page?$query_string\">\n");

echo("<textarea id=\"page\" name=\"page\" rows='25' cols='80'>\n");

echo(htmlspecialchars($text));

echo("</textarea>\n");

echo("<input type=\"submit\" value=\"Save\"/>\n");

echo("</form>\n");
 

// load the footer and close out the PHP script.

include("footer.html");
 

?>

Open in new window

0
Comment
Question by:LearningPHPMySQL
  • 3
  • 2
5 Comments
 
LVL 5

Expert Comment

by:prokvk
ID: 24290212
The script beginning with "if (get_magic_quotes_gpc()) {" should go to your action (form action) script before you use the _POST, _GET or whatever values. You can also use just the function - stripslashes on your frontend in order to get rid of the slashes ..
0
 

Author Comment

by:LearningPHPMySQL
ID: 24290237
how would i add that to the form please?  

Sorry, I'm a bit thick, still learning : (
0
 
LVL 5

Expert Comment

by:prokvk
ID: 24290253
You dont add it to a form .. you process the form in the action script (form action="THIS_IS_THE_SCRIPT_IM_TALKING_ABOUT"). In this script before you add the TinyMCE output to the DB use the code you pasted, or simply function stripslashes($tinyMceCode);

:)
0
 

Author Comment

by:LearningPHPMySQL
ID: 24290625
Like this ?
if (get_magic_quotes_gpc()) {

function stripslashes_deep($tinyMCE)

    {

        $value = is_array($tinyMCE) ?

                    array_map('stripslashes_deep', $tinyMCE) :

                    stripslashes($tinyMCE);
 

        return $tinyMCE;

    }

    $_POST = array_map('stripslashes_deep', $_POST);

    $_GET = array_map('stripslashes_deep', $_GET);

    $_COOKIE = array_map('stripslashes_deep', $_COOKIE);

    $_REQUEST = array_map('stripslashes_deep', $_REQUEST);

}

    

$this_page = $_SERVER['PHP_SELF'];

$query_string = $_SERVER['QUERY_STRING'];

echo("<form method=\"post\" action=\"$tinyMCE?$tinyMceCode?$this_page?$query_string\">\n");

echo("<textarea id=\"page\" name=\"page\" rows='25' cols='80'>\n");

echo(htmlspecialchars($text));

echo("</textarea>\n");

echo("<input type=\"submit\" value=\"Save\"/>\n");

echo("</form>\n");
 

// load the footer and close out the PHP script.

include("footer.html");
 

?>

Open in new window

0
 
LVL 5

Accepted Solution

by:
prokvk earned 500 total points
ID: 24290654
In the line:

echo("<form method=\"post\" action=\"$tinyMCE?$tinyMceCode?$this_page?$query_string\">\n");

there's the action, I don't know from this what the action exactly is, but you have to find that script file and put it there ... Consider also the alternative I gave you to change the link values only on frontend, it can be easier for you. Just run your content variable with TinyMCE (the one that contains "bad" links) through the stripslashes function like this:

$cont = stripslashes($cont);

This should work! :)
0

Featured Post

Easy Project Management (No User Manual Required)

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now