Jump to content

Does It Exist A Simple Code For This?


ckrudelux
 Share

Recommended Posts

Is where a simple code for making html/php or other lang.. codes to text?example:if I write this in my input field and post it.

<a herf="index.php">sometext</a>
output I don't want:
sometext
output I want:
<a herf="index.php">sometext</a>
Link to comment
Share on other sites

If you want to have the actual code output onto an html page, then you would need to convert the special html characters into "html entities" so they do not get converted into code.In particular, these ones are used to replace the opening and closing brackets of the html tags:

<   < 	less-than sign>  > 	greater-than sign

Link to comment
Share on other sites

To put what jlhaslip says in an exmaple, in your HTML or PHP output, write

<a herf="index.php">sometext</a>

From PHP specifically, you can also use the htmlspecialchars() function, like

echo htmlspecialchars('<a herf="index.php">sometext</a>');

which will turn the string in the function to the entities you'd otherwise write manually as in the first example.

Link to comment
Share on other sites

To put what jlhaslip says in an exmaple, in your HTML or PHP output, write
<a herf="index.php">sometext</a>

From PHP specifically, you can also use the htmlspecialchars() function, like

echo htmlspecialchars('<a herf="index.php">sometext</a>');

which will turn the string in the function to the entities you'd otherwise write manually as in the first example.

This code was what I was looking for thanks alot :) "htmlspecialchars()"
Link to comment
Share on other sites

Need help with this things as well.code what checks if there is any special chars with an output: true / falsehave text from a database what has linebreaks but if html is going to have that structure I need to add <br /> in the end of every line, any good ideas?how it will look in my page source code then I have got it from my database:

some text some textsome textsome textsome textsome textsome textsome textsome textsome textsome textsome textsome textsome textsome textsome textsome textsome text
output I get without <br />
some text some text some textsome textsome text some textsome textsome text some textsome textsome text some text some textsome text some textsome text some textsome text
Link to comment
Share on other sites

Surround the output with a <pre> element. That's the neatest, easiest, fastest and most realiable option. Like:

echo '<pre>, $output, '</pre>';

Where $output is the text you're getting from your database.

Link to comment
Share on other sites

Surround the output with a <pre> element. That's the neatest, easiest, fastest and most realiable option. Like:
echo '<pre>, $output, '</pre>';

Where $output is the text you're getting from your database.

Well it worked got linebrake in the end but then the text is too long to fit on one row it don't change row and countinues on the next one :)
Link to comment
Share on other sites

Well it worked got linebrake in the end but then the text is too long to fit on one row it don't change row and countinues on the next one :)
OK, fine, nl2br() is your next best bet:
echo nl2br($output);

If this same output is the one containing code to be displayed as is, you need to use htmlspecialchars() within nl2br(), like

echo nl2br(htmlspecialchars($output));

You'll otherwise create "<br />" in the user visible output.

Link to comment
Share on other sites

OK, fine, nl2br() is your next best bet:
echo nl2br($output);

If this same output is the one containing code to be displayed as is, you need to use htmlspecialchars() within nl2br(), like

echo nl2br(htmlspecialchars($output));

You'll otherwise create "<br />" in the user visible output.

Thanks alot :) It's works fine and everything is in place like it should :) only one thing left code for checking if a input contains a special chars
Link to comment
Share on other sites

Thanks alot :) It's works fine and everything is in place like it should :) only one thing left code for checking if a input contains a special chars
Why do you need that anyway? You can unconditionally use htmlspecialchars(). If there are any special characters, they'll be escaped, otherwise they won't be. If you want to do more, then perhaps you should instead declare another column in your DB to have the data's datatype (plain-text or HTML).If you really want to do that though, you can do it by simply comparing the output of htmlspecialchars() with the string itself, like:
if ($output === htmlspecialchars($output)) {//$output doens't contain any HTML special characters}else {//$output contains at least one HTML special character. That doesn't mean it's valid (X)HTML.}

Again, this is not a good idea in general. I'd totally advise against it.

Link to comment
Share on other sites

Why do you need that anyway? You can unconditionally use htmlspecialchars(). If there are any special characters, they'll be escaped, otherwise they won't be. If you want to do more, then perhaps you should instead declare another column in your DB to have the data's datatype (plain-text or HTML).If you really want to do that though, you can do it by simply comparing the output of htmlspecialchars() with the string itself, like:
if ($output === htmlspecialchars($output)) {//$output doens't contain any HTML special characters}else {//$output contains at least one HTML special character. That doesn't mean it's valid (X)HTML.}

Again, this is not a good idea in general. I'd totally advise against it.

Need it couse the you registrate on the page your username can only be numbers and letters and I need a code to check that there aren't any symbols in it :)
Link to comment
Share on other sites

Need it couse the you registrate on the page your username can only be numbers and letters and I need a code to check that there aren't any symbols in it :)
Again. Why? Can't you just output the username with htmlspecialchars()? If there's something else (e.g. the username needs to also be fittable in a URL without URL escaping), then perhaps you could also do regular expression checks instead upon entry that matches the allowed characters, like:
if ((bool) preg_match('/^[A-Za-z0-9]+$/', $_POST['username'])) {//Continue with registration. The username contains only latin letters and numbers.}else {//Output an error message, as the username contains something else too.}

Link to comment
Share on other sites

Again. Why? Can't you just output the username with htmlspecialchars()? If there's something else (e.g. the username needs to also be fittable in a URL without URL escaping), then perhaps you could also do regular expression checks instead upon entry that matches the allowed characters, like:
if ((bool) preg_match('/^[A-Za-z0-9]+$/', $_POST['username'])) {//Continue with registration. The username contains only latin letters and numbers.}else {//Output an error message, as the username contains something else too.}

That works too :) thanks yet again :)
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...