_SAi_ Posted February 20, 2007 Share Posted February 20, 2007 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!! Quote Link to comment Share on other sites More sharing options...
skali Posted February 20, 2007 Share Posted February 20, 2007 try replacing '\r\n' with '\n' Quote Link to comment Share on other sites More sharing options...
_SAi_ Posted February 21, 2007 Author Share Posted February 21, 2007 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? Quote Link to comment Share on other sites More sharing options...
tom100 Posted February 21, 2007 Share Posted February 21, 2007 try $text = explode("\n", $msg); Quote Link to comment Share on other sites More sharing options...
_SAi_ Posted February 21, 2007 Author Share Posted February 21, 2007 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. Quote Link to comment Share on other sites More sharing options...
tom100 Posted February 21, 2007 Share Posted February 21, 2007 Is there any way you can use post instead of get? Quote Link to comment Share on other sites More sharing options...
effigy Posted February 21, 2007 Share Posted February 21, 2007 I think new lines are %0a when using GET. Quote Link to comment Share on other sites More sharing options...
_SAi_ Posted February 21, 2007 Author Share Posted February 21, 2007 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/ Quote Link to comment Share on other sites More sharing options...
tom100 Posted February 21, 2007 Share Posted February 21, 2007 Ok, the problem is that the URL apparently can't handle returns. You will need to find some way of using POST. Otherwise, you'll have to replace the \n with something that can be sent to the URL. Quote Link to comment Share on other sites More sharing options...
effigy Posted February 21, 2007 Share Posted February 21, 2007 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. Quote Link to comment Share on other sites More sharing options...
_SAi_ Posted February 21, 2007 Author Share Posted February 21, 2007 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 Quote Link to comment Share on other sites More sharing options...
_SAi_ Posted February 21, 2007 Author Share Posted February 21, 2007 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 Quote Link to comment Share on other sites More sharing options...
effigy Posted February 21, 2007 Share Posted February 21, 2007 Single quotes create literals; use double quotes. Quote Link to comment Share on other sites More sharing options...
tom100 Posted February 21, 2007 Share Posted February 21, 2007 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. ) Quote Link to comment Share on other sites More sharing options...
effigy Posted February 21, 2007 Share Posted February 21, 2007 The same with GET: <pre> <?php echo $_GET['text'], '<br>'; print_r(explode("\n", $_GET['text'])); ?> <form method="get" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <textarea name="text"></textarea> <input type="submit"/> </form> </pre> Quote Link to comment Share on other sites More sharing options...
Archadian Posted February 21, 2007 Share Posted February 21, 2007 would this work? $nl = "<br>"; Quote Link to comment Share on other sites More sharing options...
_SAi_ Posted February 21, 2007 Author Share Posted February 21, 2007 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! Quote Link to comment Share on other sites More sharing options...
tom100 Posted February 21, 2007 Share Posted February 21, 2007 explode takes out the delimiter so, PP will be extracted from the text. As for \n not working, are you certain everything is the same, cause if the arrays are the same, then it should work. Quote Link to comment Share on other sites More sharing options...
_SAi_ Posted February 21, 2007 Author Share Posted February 21, 2007 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! Quote Link to comment Share on other sites More sharing options...
_SAi_ Posted February 21, 2007 Author Share Posted February 21, 2007 Bueller? Bueller? Anyone have any more ideas? Would I be able to replace the \n with something and explode for that? Quote Link to comment Share on other sites More sharing options...
_SAi_ Posted February 23, 2007 Author Share Posted February 23, 2007 So, does this mean that what I am trying to do is not possible? ??? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.