Jump to content

Help: Cannot detect newlines properly from form


_SAi_

Recommended Posts

Hello,

 

For the life of me, I am not able to detect a newline entered into a textarea.  I want to take in a multi-line phrase and split into seperate lines, then output an image broken into thet proper lines.  No matter what I try i cannot get it to work.

 

<?php
header("Content-type: image/png");

$nl = "\r\n";

if (!isset($_GET["text1"]))
$msg = "sample text in here are you ready?!!";
else
$msg = $_GET["text1"];

$text = split($nl, $msg);



$width=500;
$line_height=30;
$height=sizeof($text)*$line_height;

// Create the image
$im = imagecreatetruecolor($width, $height);

// Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 225, 225, 225);
$blue = imagecolorallocate($im, 0, 62, 126);
imagefilledrectangle($im, 0, 0, $width, $height, $white);

// Replace path by your own font path
$font = 'arial.ttf';

for($i=0;$i<=sizeof($text);$i++) {
//Center the text
$size = imagettfbbox(20, 0, $font, $text[$i]);
$long_text = $size[2]+$size[0];
$posx=($width-$long_text)/2;
// Add the text
imagettftext($im, 20, 0, $posx, $line_height+$line_height*$i, $blue, $font, $text[$i]);
}

imagepng($im);
imagedestroy($im);
echo "<br><br>" + $text[1];
?>

 

As it works now, the lines are not found and it all outputs to a single line.

 

Here is my non-working page so far...

http://68.147.219.153/fontpreview/test/close/

 

Thanks for any help!!

Link to comment
Share on other sites

I tried replacing with '\n' but that didn't work.  It still puts it all onto one line... except the default text.. it breaks that up kinda strangely.

 

$nl = '\n';

if (!isset($_GET["text1"]))
$msg = "multi line centered text";
else
$msg = $_GET["text1"];

$text = split($nl, $msg);

 

Any other suggestions?

Link to comment
Share on other sites

Thanks, but that  still doesn't work for me.

 

//$nl = '\n';

if (!isset($_GET["text1"]))
$msg = "multi line centered text";
else
$msg = $_GET["text1"];

//$text = split($nl, $msg);
$text = explode("\n", $msg);

 

Everything is back onto a single line.

Link to comment
Share on other sites

ok, I was not able to get a POST instead of a GET.  It just showed my default text eventhough when I echoed the variable it showed the new text.

 

I tried changing from \n to %0a and still the same.... only a single line of text.

 

 

$nl = '%0a';

if (!isset($_GET["text1"]))
$msg = "multi line centered text";
else
$msg = $_GET["text1"];

$text = split($nl, $msg);

 

Is there any way to show all of the characters in the string so I know exactly what I need to split by?

As is it now I get a space where a carriage return is.

 

http://68.147.219.153/fontpreview/test/close/

Link to comment
Share on other sites

I tried changing from \n to %0a and still the same.... only a single line of text.

 

How about %0A?

 

Is there any way to show all of the characters in the string so I know exactly what I need to split by?

 

Try echo $_GET["text1"];, then use your browser to view the source.

 

P.S. The link you provided is using POST, not GET. If you're trying to create a string to pass to GET, use urlencode.

Link to comment
Share on other sites

I changed all of my _GET to _POST and changed the form method="post".  All I see in the image is my default text.  Am I missing something?

 

What is the proper way to look for a \n... with " " or with ' '?

 

Again if I use '\n' my lines are split at every n rather than my intended \n.

 

$nl = '\n';

if (!isset($_POST["text1"]))
$msg = "multi line centered text";
else
$msg = $_POST["text1"];

$text = split($nl, $msg);

 

and

 

<form name="form1" method="post" action="index.php">

<?php
if(!empty($_POST['text1'])) {
    $img = '_2splitb.php?text1=' . $_POST['text1'];
} else {
    $img = '_2splitb.php';
}
echo "<img src=\"$img\" />";

echo "<br><br>";
echo $_POST['text1'];


?>

 

http://68.147.219.153/fontpreview/test/close/index.php

Link to comment
Share on other sites

also, on the _GET files I did the echo and looked at the source... got this

 

<html>

<form name="form1" method="get" action="index.php">

<img src="_2splitb.php?text1=this
is" /><br><br>this
is

  <p>
    <br>
    <br>
    <textarea name="text1" cols="50" id="text1">this
is</textarea>
  </p>
  <p>

    <input type="submit" name="Submit" value="Submit">
</p>
</form>
</html>

 

It doesn't show me anything special.

 

_GET: http://68.147.219.153/fontpreview/test/close/

_POST http://68.147.219.153/fontpreview/test/close/index-post.php

Link to comment
Share on other sites

I just tested the below script and it works fine: If you make your script work in the same manor it should also work fine.

<?php
if (isset($_POST['message']))
	{
	$message=explode("\n", $_POST['message']);
	print_r($message);
	}
?>
<form method="POST" action="test.php">
<textarea name="message" rows=4 cols=20>
</textarea>
<input type="submit" value="Submit">
</form>

Array ( [0] => This is a test. [1] => Its a fun test. [2] => Each statement is a new line. )

Link to comment
Share on other sites

Things still aren't creating the image the way it should...

 

If I explode for say "pp" and enter happybirthday... the array shows correctly and the image shows it broken into two lines with the pp gone.  Now, I'm not familiar with the explode function so I don't know if that's what it is supposed to do.

 

On the other hand, if I put back in my "\n" into the explode function, all of a sudden things stop working.

 

 

The "pp" Form:

<html>

<form name="form1" method="get" action="<?php echo $_SERVER['PHP_SELF']; ?>">

<?php
if(!empty($_GET['text1'])) {
    $img = '_2splitb-pp.php?text1=' . $_GET['text1'];
} else {
    $img = '_2splitb-pp.php';
}
echo "<img src=\"$img\" />";

echo "<br><br>";
echo $_GET['text1'];
echo "<br><br>";

print_r(explode("pp", $_GET['text1']));


?>


  <p>
    <br>
    <br>
    <textarea name="text1" cols="50" id="text1"><?php echo($_GET['text1']); ?></textarea>
  </p>
  <p>
    <input type="submit" name="Submit" value="Submit">
</p>
</form>
</html>

 

The "pp" Image Creation:

 

<?php
header("Content-type: image/png");

if (!isset($_GET["text1"]))
$msg = "multi line centered text";
else
$msg = $_GET["text1"];

$text = explode("pp", $msg);

$width=500;
$line_height=30;
$height=sizeof($text)*$line_height;

// Create the image
$im = imagecreatetruecolor($width, $height);

// Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 225, 225, 225);
$blue = imagecolorallocate($im, 0, 62, 126);
imagefilledrectangle($im, 0, 0, $width, $height, $white);

// Replace path by your own font path
$font = 'arial.ttf';

for($i=0;$i<=sizeof($text);$i++) {
//Center the text
$size = imagettfbbox(20, 0, $font, $text[$i]);
$long_text = $size[2]+$size[0];
$posx=($width-$long_text)/2;
// Add the text
imagettftext($im, 20, 0, $posx, $line_height+$line_height*$i, $blue, $font, $text[$i]);
}

imagepng($im);
imagedestroy($im);
echo "<br><br>" + $text[1];
?>

 

 

Here is the "pp" example:

http://68.147.219.153/fontpreview/test/close/index-pp.php?text1=happybirthday&Submit=Submit

 

Here is the same program with the "pp" replaced with "\n":

http://68.147.219.153/fontpreview/test/close/index.php?text1=happy%0D%0Abirthday&Submit=Submit

 

The only things changed are the "pp" replaced with the "\n".  So, how come the array shows two separate words but the image still does not break it into the two lines?

 

I am soooo confused!

Link to comment
Share on other sites

Everything is the same... all I did was copy the lines of code and change the "pp" to "\n" and remark the other lines of code.  If someone could take this code and test for themselves that would be great!

 

form:

<html>

<form name="form1" method="get" action="<?php echo $_SERVER['PHP_SELF']; ?>">

<?php
if(!empty($_GET['text1'])) {
    $img = '_2splitb.php?text1=' . $_GET['text1'];
} else {
    $img = '_2splitb.php';
}
echo "<img src=\"$img\" />";

echo "<br><br>";
echo $_GET['text1'];
echo "<br><br>";
print_r(explode("\n", $_GET['text1']));
//print_r(explode("pp", $_GET['text1']));


?>


  <p>
    <br>
    <br>
    <textarea name="text1" cols="50" id="text1"><?php echo($_GET['text1']); ?></textarea>
  </p>
  <p>
    <input type="submit" name="Submit" value="Submit">
</p>
</form>
</html>

 

Image Creation:

<?php
header("Content-type: image/png");

if (!isset($_GET["text1"]))
$msg = "multi line centered text";
else
$msg = $_GET["text1"];

$text = explode("\n", $msg);
//$text = explode("pp", $msg);

$width=500;
$line_height=30;
$height=sizeof($text)*$line_height;

// Create the image
$im = imagecreatetruecolor($width, $height);

// Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 225, 225, 225);
$blue = imagecolorallocate($im, 0, 62, 126);
imagefilledrectangle($im, 0, 0, $width, $height, $white);

// Replace path by your own font path
$font = 'arial.ttf';

for($i=0;$i<=sizeof($text);$i++) {
//Center the text
$size = imagettfbbox(20, 0, $font, $text[$i]);
$long_text = $size[2]+$size[0];
$posx=($width-$long_text)/2;
// Add the text
imagettftext($im, 20, 0, $posx, $line_height+$line_height*$i, $blue, $font, $text[$i]);
}

imagepng($im);
imagedestroy($im);
echo "<br><br>" + $text[1];
?>

 

Thanks!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.