Pretty simple but I'm looking for the easiest way (HEX?) and it's not working...
I want to add to the string backspaces (delete last character)...
Here is my simple code :
<?php
echo '<br>Delete me!!!'."\x8"."\x8"."\x8"."\x8"."\x8"."\x8"."\x8"."\x8"."\x8"."\x8"."\x8"."\x8";
?>
Small tweak needed here ;)
backspace is a special character that is interpreted by editors and various text input boxes as a request to remove the character before the cursor. The backspace character itself has no magical powers, it cannot make disappear something that was already displayed.
If you need to remove some characters from a string in PHP you can use substring(), str_replace(), preg_replace() or other string handling function.
For example, you can ignore the last 3 characters from a string like this:
echo(substr('blah-blah', 0, -3));
Related
I have an issue. I'm trying to write a string with ASCII text like this: '/\'. But whenever I do that the backslash screws up the code by canceling out the quote defining it a string therefore screwing it up. Is there anyway to cancel out the backslash so it doesn't cancel out the quote? Thanks guys!
The \ is special character, that says: 'The next character has special meaning'.
So if you want to dispaly \ you should write... \\ to get one \ in output
It would be very helpful to show what you have tried, but this will produce the exact output you requested (as shown by SO)
echo '\'/\\' . "'\n" ;
'/\'
It should also give you an idea of how backslash escaping works in different types of strings.
A great solution when writing stuff like that is HEREDOC. Inside a heredoc block you don't need to worry about escaping anything, it will just be text.
For example:
echo <<<TEXT
/|\/|\/|\/|\/|\/|\/|\/|\/|\/|\
TEXT;
There is one catch. PHP will break if you don't align the echo at the start of the line, or if the TEXT; is not aligned at the start of the line.
Heredoc can also be assigned to a variable, like so:
$var = <<<SOME_MORE_TEXT
/|\/|\/|\/|\/|\/|\/|\/|\/|\/|\
SOME_MORE_TEXT;
Finally, HEREDOC preserves tabs and spaces. Which also might come in handy when doing ASCII art.
Refer to: http://php.net/manual/en/language.types.string.php for more information.
You only need to escape the final one when using single quotes.
$var = 'backslash\backslash\backslash\\';
// output is:
// backslash\backslash\backslash\
in my current template, i have a large block of text that comes in from the user. the block is then split at the 600 character mark and placed into a specific div on the page, the rest of the block is placed near the bottom of the page. this works fine, but a problem arises when there are a lot of links added near the beginning of the post.
the <a href='...'></a> is being counted towards the total character count. i always want to split the block at 600.
is there an easy way for me to find out the length of the html characters? that way I can just add them to the split_length. or any other ideas? ways to ignore the html characters entirely?
edit:
i should be clear i WANT the links to stay...
Use PHP's function strip_tags() and then count the characters.
You can remove all the HTML code, and split your text at the exact count. For that, use:
string strip_tags ( string $str [, string $allowable_tags ] )
This function tries to return a string with all NUL bytes, HTML and
PHP tags stripped from a given str. It uses the same tag stripping
state machine as the fgetss() function.
See here for more info.
Every time I try to echo a string there is no new line. I how can I make a newline when calling echo in php using the $_GET?
here is my code:
<?php
$text = "Hello world";
$text2 = $_GET['msg'];
echo $text2
?>
and this is what I enter in the url:
http://localhost/hello.php?msg=hello%0Dworld
or this one:
http://localhost/hello.php?msg=hello%0Aworld
and even this one:
http://localhost/hello.php?msg=hello%0D%0Aworld
The echo has to be a newline please don't say I should use a different method than $_GET. It has to be $_GET
While performing your exercises you are creating an HTML page.
HTML is a special markup language, which renders according to set of rules, some of them are:
<> characters has a special meaning of control structures named tags
all newline characters are ignored
to make a newline on the page, one have to use suitable tag - such as <br>, <p> or whatever.
So, to make a newline appear on your page, you have to convert newline characters to tags. Either use nl2br() function to get a <br /> tag or str_replace() if you want any other one
Be aware that echoing any request variables without validating them is a considerable security risk! If you want to publish any application with this code it needs to be redesigned.
As common sense states, the conversion from urlencoded to the corresponding character is automatically done by php, but HTML does not render such characters, so you either need to convert them into linebreaks or enclose the message in <pre> tags.
I'm using PHP's function to count the number of words from a textarea via POST...
The issue is that if I do a post back to my file and output the word count it is different than if I copy and paste the same text into my PHP script to evaluate the word count.
What is throwing off the number? There is difference of 6 words, incidentally there are 6 double line breaks in the textarea as well.
How do I minimize this difference?
You could remove the line breaks and tags altogether:
str_word_count(str_replace('<br>', '', nl2br(strip_tags($data))));
Or I guess this is better:
str_word_count(strip_tags(nl2br($data)));
If your line breaks are in HTML-form, you could use something like strip_tags()
If they aren't, I suspect an issue with encoding. Maybe an combination of stripslashes, utf8_encode or utf8_decode could solve this wrong counted words.
As an last resort you could use some regular expression to filter anything but [a-zA-Z] and spaces.
I need to replace characters in a string with their HTML coding.
Ex. The "quick" brown fox, jumps over the lazy (dog).
I need to replace the quotations with the & quot; and replace the brakets with & #40; and & #41;
I have tried str_replace, but I can only get 1 character to be replaced. Is there a way to replace multiple characters using str_replace? Or is there a better way to do this?
Thanks!
I suggest using the function htmlentities().
Have a look at the Manual.
PHP has a number of functions to deal with this sort of thing:
Firstly, htmlentities() and htmlspecialchars().
But as you already found out, they won't deal with ( and ) characters, because these are not characters that ever need to be rendered as entities in HTML. I guess the question is why you want to convert these specific characters to entities? I can't really see a good reason for doing it.
If you really do need to do it, str_replace() will do multiple string replacements, using arrays in both the search and replace paramters:
$output = str_replace(array('(',')'), array('(',')'), $input);
You can also use the strtr() function in a similar way:
$conversions = array('('=>'(', ')'=>')');
$output = strtr($conversions, $input);
Either of these would do the trick for you. Again, I don't know why you'd want to though, because there's nothing special about ( and ) brackets in this context.
While you're looking into the above, you might also want to look up get_html_translation_table(), which returns an array of entity conversions as used in htmlentities() or htmlspecialchars(), in a format suitable for use with strtr(). You could load that array and add the extra characters to it before running the conversion; this would allow you to convert all normal entity characters as well as the same time.
I would point out that if you serve your page with the UTF8 character set, you won't need to convert any characters to entities (except for the HTML reserved characters <, > and &). This may be an alternative solution for you.
You also asked in a separate comment about converting line feeds. These can be converted with PHP's nl2br() function, but could also be done using str_replace() or strtr(), so could be added to a conversion array with everything else.