How to unescape in php?

I am trying to save foreign characters (e.g. أوه يمارس الجنس) through javascript to php and then stored in a mysql database.

I am able to do this but only if I do not escape() the string trying to be saved in javascript before sending to php.

Now for the problem.

Without an escaped string certain characters cannot be saved through my ajax type function due to the ampersand being seperators.
This is easily overcome by simply doing the escape() BUT the certain characters (e.g. أوه يمارس الجنس) will then not be ‘unescaped’ correctly in php.

How can I overcome this, or better yet ‘unescape’ it somehow properly in php?

The urldecode function should see you right.

I tried this on a string “(e.g. أوه يمارس الجنس) t”.
The data was decoded as “(e.g. %u0623%u0648%u0647 %u064A%u0645%u0627%u0631%u0633 %u0627%u0644%u062C%u0646%u0633) t”.

Perhaps some code will show the flaw…

Consists of 3 parts:
1.) index page
2.) ajax php
3.) sql file

1.)


<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
		<script language="javascript" type="text/javascript">
			function saveIt() {
				var title = escape(document.getElementById("title").value);
				var content = escape(document.getElementById("content").value);
				document.getElementById("page_list").innerHTML = ajax("saveNew.php", "title="+title+"&content="+content);
			}
			function ajax(url, parameters) {
				http_request = false;
				if( window.XMLHttpRequest ){ // Mozilla, Safari, ...
					http_request = new XMLHttpRequest();
					if( http_request.overrideMimeType ) http_request.overrideMimeType('text/html');
				} else if( window.ActiveXObject ){ // IE
					try {
						http_request = new ActiveXObject("Msxml2.XMLHTTP");
					} catch (e){
						try {
							http_request = new ActiveXObject("Microsoft.XMLHTTP");
						} catch (e){}
					}
				}
				if( !http_request ){
					alert('ERROR: cannot execute ajax...');
					return false;
				}
				http_request.open('POST', url, false);
				http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
				http_request.setRequestHeader("Content-length", parameters.length);
				http_request.setRequestHeader("Connection", "close");
				http_request.send(parameters);
				if( http_request.readyState == 4 && http_request.status == 200 ){
					return http_request.responseText;
				} else return false;
			}
		</script>
		<title></title>
	</head>
	<body>
		<div style="border: 1px solid #000">
			<p>title<br /><input type="text" id="title" /></p>
			<p>content<br /><textarea rows="10" id="content"></textarea></p>
			<p><input type="button" onclick="saveIt()" value="Save" /></p>
		</div>
		<div id="page_list">
			&nbsp;
		</div>
	</body>
</html>

2.)


&lt;?php

	$title = urldecode($_POST["title"]);
	$content = urldecode($_POST["content"]);

	mysql_connect("localhost", "root", "") or die("ERROR: CONNECTION TO DATABASE");
	mysql_select_db("utf8test") or die("ERROR: SELECT DATABASE");
	
	mysql_query("INSERT INTO atable (`title`, `content`) VALUES('".$title."', '".$content."')");
			
	$query = mysql_query("SELECT * FROM atable");
	$str="";
	while ($result = mysql_fetch_object($query)) {
		$str .= $result-&gt;content."&lt;br/&gt;";
	}
	echo $str;
?&gt;

3.)
CREATE DATABASE utf8test DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;
USE utf8test;


CREATE TABLE atable (
id int(11) NOT NULL auto_increment,
title varchar(255) collate utf8_unicode_ci NOT NULL default ‘’,
content varchar(255) collate utf8_unicode_ci NOT NULL default ‘’,
PRIMARY KEY (id)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;

smftre: I faced a similar situation. In my case it was text in Thai language and after using urldecode, I never got back the original text… all of it was in the same %udddd format.

However, digging around a bit on the urldecode manual page I found a solution that has worked for me so far without a hitch.

I would have posted the exact link to the solution here… but the vBulletin’s refusing to let me add links.

You’ll have to use a custom function which will run a urldecode and a html_entity_decode consecutively to get back the original text…

I’m quoting the function here for you reference…


  function utf8_urldecode($str) {
    $str = preg_replace("/%u([0-9a-f]{3,4})/i","&#x\\\\1;",urldecode($str));
    return html_entity_decode($str,null,'UTF-8');;
  }

See if this works out for you :slight_smile:

Cheers,
m^e

Try the link like below?

www . website . com

:wink: