Home » Php » php – Losing \n In String Through GET

php – Losing \n In String Through GET

Posted by: admin February 25, 2020 Leave a comment

Questions:

When I pass a \n in a string to this code through a link, it is getting lost in the Home and Splash portion of the conditional but if I hard-code the $String value to include it, it works as it should

For example using $String=$GETvalue; when there is a \n in the $GETvalue, will show Some text\nMore text but if I use $String=”Some text\nMore text”; it shows:

Some text
More text

as it should. How can I maintain the \n through a GET?

<?php  
$FontPath = "/internals/truetype/";
$FontName = "vineritc.ttf";
$EnableShadow=TRUE;
$CenterAlign=TRUE;

$GETkey = key($_GET);
$GETvalue = current($_GET);

switch ($GETkey):
    case "AuthorID":
        $Year = date("Y");
        $AuthorName = DBLookup("SELECT `AuthorName` FROM authorbiographies WHERE `ID`=$GETvalue",$siteDB);
        $String = "Copyright &copy; $Year\n$AuthorName";
        $FontSize = 10;
        $Angle = 0;
    break;
    case "CategoryID":
        $String = DBLookup("SELECT `CategoryName` FROM categories WHERE `ID`=$GETvalue",$siteDB);
        $FontSize = 15;
        $Angle = 0;
    break;
    case "Home":
        $String = $GETvalue;
        $FontSize = 28;
        $Angle = 0;
    break;
    case "Splash":
        $String = $GETvalue;
        $FontSize = 38;
        $Angle = 0;
    break;
endswitch;

$TextImage = textimage();

header("Pragma: no-cache");
header('Content-Type: image/png');

echo $TextImage;
?>

This code is called with:

<img src="/viewers/show.php?Splash=Some+text\nMore+text!">
How to&Answers:

Problem solved! I was sending the string as (shortened here without alt or title:

<img src="/viewers/show.php?Splash=Some+text\nMore+text!">

when it should have been sent as:

<img src="/viewers/show.php?Splash=<?=urlencode("Some text\nMore text!");?>">

No decoding is apparently needed on the receiving end. Odd, I snapped awake in the middle of the night with the answer after fighting it for so long! It was sending it urlencoded() originally, then someone here in another area told me it was unnecessary so I removed the urlencode() from it, then forgot I had done so.