Form that gets text from another site, Spaces in textarea

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
YotamElal
Forum Newbie
Posts: 5
Joined: Sun Aug 20, 2006 1:00 pm

Form that gets text from another site, Spaces in textarea

Post by YotamElal »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I am creating a translation page that gets the text from a url which the query strings are past to.
When the user submits the form the text is sent to the querystring but the spaces are converted to plus-characters.
When I use curl with the $text string variable that contains plus-characters, it ignores the variable, and the url gets an empty query string.

Can I change the submit to create underscores instead of spaces?
If not then can I make curl support a string with plus-characters?
Or is there another way to do this?

Here is the translation page:
[url=http://www.concisefreeware.com/translate.php]http://www.concisefreeware.com/translate.php[/url]

Here is the code:

Code: Select all

<?php function a2() { if ($_GET['theme'] == "w") print ("b"); else print ("w"); } 
	function a3() { if ($_GET['theme'] == "w") print ("w"); else print ("b"); } 
	function text() { print str_replace("_"," ",$_GET['text']); } 
	function data() { 
	$langfrom = $_GET['langfrom'];
	$langto = $_GET['langto'];
	$text = str_replace("_","%20",$_GET['text']);
	$url = "http://www.worldlingo.com/S1889.1/api?wl_data=$text;&wl_srclang=$langfrom;&wl_trglang=$langto;&wl_password=secret";
	$curl_handle = curl_init($url);
	curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT,10);
	curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
	$content = "";
	$content = curl_exec($curl_handle);
	curl_close($curl_handle);
	$content = str_replace("0","",$content);
	switch ($langfrom) {
		case "en": if ($langto == en) $content = ""; break;
		case "ar": if ($langto == ar) $content = ""; break;
		case "zh_CN": if ($langto == zh_CN) $content = ""; break;
		case "zh_TW": if ($langto == zh_TW) $content = ""; break;
		case "nl": if ($langto == nl) $content = ""; break;
		case "fr": if ($langto == fr) $content = ""; break;
		case "de": if ($langto == de) $content = ""; break;
		case "el": if ($langto == el) $content = ""; break;
		case "ja": if ($langto == ja) $content = ""; break;
		case "ko": if ($langto == ko) $content = ""; break;
		case "pt": if ($langto == pt) $content = ""; break;
		case "ru": if ($langto == ru) $content = ""; break;
		case "es": if ($langto == es) $content = ""; break;
		case "sv": if ($langto == sv) $content = ""; break;
		}
	print $content;
	}
	function check() { 
	$langfrom = $_GET['langfrom'];
	$langto = $_GET['langto'];
	$content = "";
	switch ($langfrom) {
		case "en": if ($langto == en) $content = "Please select two different languages.<br><br>"; break;
		case "ar": if ($langto == ar) $content = "Please select two different languages.<br><br>";  break;
		case "zh_CN": if ($langto == zh_CN) $content = "Please select two different languages.<br><br>";  break;
		case "zh_TW": if ($langto == zh_TW) $content = "Please select two different languages.<br><br>";  break;
		case "nl": if ($langto == nl) $content = "Please select two different languages.<br><br>";  break;
		case "fr": if ($langto == fr) $content = "Please select two different languages.<br><br>";  break;
		case "de": if ($langto == de) $content = "Please select two different languages.<br><br>";  break;
		case "el": if ($langto == el) $content = "Please select two different languages.<br><br>";  break;
		case "ja": if ($langto == ja) $content = "Please select two different languages.<br><br>";  break;
		case "ko": if ($langto == ko) $content = "Please select two different languages.<br><br>";  break;
		case "pt": if ($langto == pt) $content = "Please select two different languages.<br><br>";  break;
		case "ru": if ($langto == ru) $content = "Please select two different languages.<br><br>";  break;
		case "es": if ($langto == es) $content = "Please select two different languages.<br><br>";  break;
		case "sv": if ($langto == sv) $content = "Please select two different languages.<br><br>";  break;
		}
	print $content;
	}
	function selectFrom($lang) { 
	$langfrom = $_GET['langfrom'];
	if ($lang == $langfrom) print "selected=\"selected\"";
	}
	function selectTo($lang) { 
	$langto = $_GET['langto'];
	if ($lang == $langto) print "selected=\"selected\"";
	}
	?>

<b><big><? check(); ?></big></b>
<FORM ACTION="translate.php" METHOD=GET>Translated Text:<br>
<textarea rows="6" cols="50">
<? data(); ?>
</textarea>
<br><br>
Original Text:<br>
<textarea name="text" rows="6" cols="50">
<? text(); ?>
</textarea><br><br>
<select name="langfrom">
<option value="en" <? selectFrom("en"); ?>>English
<option value="ar" <? selectFrom("ar"); ?>>Arabic
<option value="zh_TW" <? selectFrom("zh_TW"); ?>>Chinese Simplified
<option value="zh_CN" <? selectFrom("zh_CN"); ?>>Chinese Traditional
<option value="nl" <? selectFrom("nl"); ?>>Dutch
<option value="fr" <? selectFrom("fr"); ?>>French
<option value="de" <? selectFrom("de"); ?>>German
<option value="el" <? selectFrom("el"); ?>>Greek
<option value="it" <? selectFrom("it"); ?>>Italian
<option value="ja" <? selectFrom("ja"); ?>>Japanese
<option value="ko" <? selectFrom("ko"); ?>>Korean
<option value="pt" <? selectFrom("pt"); ?>>Portuguese
<option value="ru" <? selectFrom("ru"); ?>>Russian
<option value="es" <? selectFrom("es"); ?>>Spanish
<option value="sv" <? selectFrom("sv"); ?>>Swedish
</select>
to
<select name="langto">
<option value="en" <? selectTo("en"); ?>>English
<option value="ar" <? selectTo("ar"); ?>>Arabic
<option value="zh_TW" <? selectTo("zh_TW"); ?>>Chinese Simplified
<option value="zh_CN" <? selectTo("zh_CN"); ?>>Chinese Traditional
<option value="nl" <? selectTo("nl"); ?>>Dutch
<option value="fr" <? selectTo("fr"); ?>>French
<option value="de" <? selectTo("de"); ?>>German
<option value="el" <? selectTo("el"); ?>>Greek
<option value="it" <? selectTo("it"); ?>>Italian
<option value="ja" <? selectTo("ja"); ?>>Japanese
<option value="ko" <? selectTo("ko"); ?>>Korean
<option value="pt" <? selectTo("pt"); ?>>Portuguese
<option value="ru" <? selectTo("ru"); ?>>Russian
<option value="es" <? selectTo("es"); ?>>Spanish
<option value="sv" <? selectTo("sv"); ?>>Swedish
</select>
&nbsp;
<INPUT TYPE="submit" VALUE="Translate">
</FORM>

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Post by daedalus__ »

Code: Select all

tags please.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

This is a POST submission, change your form type to POST otherwise there will be a limitation of 4096 bytes on the text.

What is with these lines?:

Code: Select all

$text = str_replace("_","%20",$_GET['text']);

Code: Select all

function text() { print str_replace("_"," ",$_GET['text']); }
urlencode() perhaps?
YotamElal
Forum Newbie
Posts: 5
Joined: Sun Aug 20, 2006 1:00 pm

Post by YotamElal »

How do I change the form type?

With this line I take the undorscores from the text-querystring and change them to spaces cause if I give a query with pluses or space then I get an error.

Code: Select all

$text = str_replace("_","%20",$_GET['text']);
There are two ways that this can be solved.
Either on form submit use underscores instead of plus characters.
Or, figure out how to make curl except pluses in the string variable.

I tried to use urlencode(), but it didn't solve the problem?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

$_GET :arrow: $_POST
method="get" :arrow: method="post"
YotamElal
Forum Newbie
Posts: 5
Joined: Sun Aug 20, 2006 1:00 pm

Post by YotamElal »

$_GET $_POST
method="get" method="post"
What do I do with this?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

YotamElal wrote:What do I do with this?
YotamElal wrote:How do I change the form type?
maybe?
YotamElal
Forum Newbie
Posts: 5
Joined: Sun Aug 20, 2006 1:00 pm

Post by YotamElal »

oops sorry I misunderstood.

The _Post function doesn't solve the problem...
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

Its not supposed to solve the problem it was a separate recommendation I made.

You have a switch which has several problems:
  • You are using a switch where arrays are probably much better
  • You are using constants en,nl,fr,de i'm quite sure you haven't defined any of these
  • You are using an if condition inside the case which is repeating what the case has already determined.
  • They are all setting content to '' so all this...

    Code: Select all

    switch ($langfrom) {
                    case "en": if ($langto == en) $content = ""; break;
                    case "ar": if ($langto == ar) $content = ""; break;
                    case "zh_CN": if ($langto == zh_CN) $content = ""; break;
                    case "zh_TW": if ($langto == zh_TW) $content = ""; break;
                    case "nl": if ($langto == nl) $content = ""; break;
                    case "fr": if ($langto == fr) $content = ""; break;
                    case "de": if ($langto == de) $content = ""; break;
                    case "el": if ($langto == el) $content = ""; break;
                    case "ja": if ($langto == ja) $content = ""; break;
                    case "ko": if ($langto == ko) $content = ""; break;
                    case "pt": if ($langto == pt) $content = ""; break;
                    case "ru": if ($langto == ru) $content = ""; break;
                    case "es": if ($langto == es) $content = ""; break;
                    case "sv": if ($langto == sv) $content = ""; break;
                    }
    ...is pretty much equivlent to this...

    Code: Select all

    $content = '';
  • and given that you are setting content to an empty string how is your code supposed to work?
YotamElal
Forum Newbie
Posts: 5
Joined: Sun Aug 20, 2006 1:00 pm

Post by YotamElal »

* You are using a switch where arrays are probably much better
For now I'll leave this.
* You are using constants en,nl,fr,de i'm quite sure you haven't defined any of these
oops, I fixed them
* You are using an if condition inside the case which is repeating what the case has already determined.
No I am not, one is langfrom and the other is langto
* and given that you are setting content to an empty string how is your code supposed to work?
It gives an empty string only when langfrom = langto

Someone told me that I need to turn magic_quotes off to solve this problem.
I don't know how to do this with my hosting company (Dreamhost).
I tried adding the line that disables magic quotes in the .htaccess but no luck...
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

No I am not, one is langfrom and the other is langto
Ah ok my mistake.
Post Reply