Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Replace characters in a variable
Old 04-23-2005, 05:32 AM Replace characters in a variable
Justin-Credible's Avatar
Novice Talker

Posts: 12
Trades: 0
Its past 2AM here and I need some expert help!

Basically I'm putting together a site that the customer will be able to edit on the web themselves using a RichText editor (so they won't have to code any html).

I've been through 5 or so different rich text editors/applets and finally found the one I want. The problem is that it is called with javascript and when I try to push my HTML from the MySQL database into the javascript code there are errors (because of quotes, apostrophes, line breaks, that javascript doesn't like).

My question is this: how do I put a \ in front of the problematic characters of some HTML that I have queried into a variable in PHP?

Here is what I have:

PHP Code:
<?

$page
=$_GET['page'];
include(
"dbinfo.inc.php");
mysql_connect("localhost",$username,$password);
@
mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM $page";
$result=mysql_query($query);

mysql_close();

$html=mysql_result($result,0,"HTML");

?>

<center><font size=5 face="Arial, Helvetica, sans-serif" color=black>Edit HTML</font></center>
<br><br>

<center>
<form action="updated.php" method="post">
<input type="hidden" name="page" value="<? echo $page?>">
HTML for <? echo $page?>:<br>

<SCRIPT LANGUAGE="javascript" SRC="../inc/HTMLRichTextArea.js"></SCRIPT>
<SCRIPT LANGUAGE="javascript" SRC="../inc/HTMLRichTextAreaFactorySettings.js"></SCRIPT>
<SCRIPT LANGUAGE="javascript">
   <!--
      var strDefaultHTML = "<? echo $html?>";
      new HTMLRichTextArea("ud_html", 80, 200, strDefaultHTML).Draw();
   //-->
</SCRIPT>



<center>
<br>
<input type="Submit" value="Update">&nbsp;&nbsp;&nbsp;
<input type="button" value="Cancel" OnClick="location.href='index.php?page=<? echo $page?>'">
</center>

</form>
The problem is occuring on this line:
var strDefaultHTML = "<? echo $html; ?>";

$html is the variable that contains raw html code that I need to fix by inserting the \'s.

I know the rest of the code works because I initially coded the page using a standard <textarea> tag, and it worked fine.
Justin-Credible is offline
Reply With Quote
View Public Profile Visit Justin-Credible's homepage!
 
 
Register now for full access!
Old 04-23-2005, 08:27 AM
leavethisplace's Avatar
Ultra Talker

Posts: 297
Trades: 0
hmm, do you mean when you call it back from the mySQL database it goes funny, cause og a string like "lot's of lovely melon's" ? Also, does it go funny if there are normal quotes in there, like "" ?

If so, you can use str_replace:

PHP Code:
// SQL should allow characters ' or ", so parse this when getting info FROM SQL
$content str_replace("'""\'"$content);   // for the ' character
$content str_replace("/"", "\/""$content); // for the " character 
EDIT: That's odd, forum is taking a character out. on the second str_replace, in the second argument, there should be a \ infront of that /

That should do it!
__________________
A lie gets halfway around the world before the truth has a chance to get its pants on. - Sir Winston Churchill

Please visit my sites:
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE

Last edited by leavethisplace; 04-23-2005 at 08:30 AM..
leavethisplace is offline
Reply With Quote
View Public Profile
 
Old 04-24-2005, 02:35 AM
Justin-Credible's Avatar
Novice Talker

Posts: 12
Trades: 0
Thanks for your help. Finally got it working. Because there were quotes and line breaks in the variable that I was feeding into the javascript it kept giving me javascript errors.

I tried the code you provided, but for some reason it didn't work. I played with it some and this is what I came up with:

PHP Code:
$html2 str_replace('"''\"'$html);
$html2 str_replace("\r\n"" "$html2); 
The first line to replace " with \"
and the second line to replace line breaks with a space
Justin-Credible is offline
Reply With Quote
View Public Profile Visit Justin-Credible's homepage!
 
Reply     « Reply to Replace characters in a variable
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.35941 seconds with 12 queries