0

With PHP echo I want to get this in the browser:

var x = "<td><a class=\"button3\"><input type=\"text\" name=\"" + a + "_" + y + "\" id=\"something\" value=\"\" class=\"inputText\"></a></td>";

How to escape this in PHP ?

4
  • you could always just place it in a text file and read the file into a variable in PHP. Or you could take the time to escape all of the special characters properly according to the PHP spec. Commented Jan 22, 2013 at 14:25
  • See PHP's json_encode() function. Commented Jan 22, 2013 at 14:25
  • I don't want to use a PHP variable in JavaScript, I just want to echo a JavaScript line. Commented Jan 22, 2013 at 14:33
  • how would you escape the special characters ? Commented Jan 22, 2013 at 14:44

1 Answer 1

2

You do not seem to be using single quotes ' so wrapping your line of code in a string is quite easy when using single quotes.

To literally show the text in your browser, you can use the htmlentities() function and echo it to output it to the browser:

echo htmlentities('var x = "<td><a class=\"button3\"><input type=\"text\" name=\"" + a + "_" + y + "\" id=\"something\" value=\"\" class=\"inputText\"></a></td>";');

It will convert all characters that are used in HTML to create tags. For example < is converted to &lt; so it displays the correct character. Please take a look at the available flags and select the ones you require for properly displaying your line of code.

Sign up to request clarification or add additional context in comments.

6 Comments

No this doesn't work, after var x = there appears a col. Look here bit.ly/WT4xzi
Your resulting code is full of <br>, you need to use echo "\n"; for newlines. After replacing (some of) them with newlines, you code looks ok to me... I do not know what you mean by ' after var x = there appears a col . '.
There is no difference between using echo "\n"; and echo "<br>";
Do you get the same like this bit.ly/VhPjcQ ? I don't want to get the col after var x =" I just want to get var x = "<td><a class=\"button3\"><input type=\"text\" name=\"" + a + "_" + y + "\" id=\"something\" value=\"\" class=\"inputText\"></a></td>"; in the browser
Updated my answer again, to show how you can show your line of code in its literal form in a browser.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.