I'm testing a preg_replace function, and I return from an ajax function the processed data (after I process the data through preg_replace, I put it through htmlentities() ):
My test string is:
pr eg123 ~!##$%^&*()-+={}|[]:;< >? "...,'/...
I'm trying to make sure all those characters aren't replaced. My replace function is:
$string = preg_replace('/[^a-zA-Z0-9\s+\n\r,.\/~!##\$%\^&*()\+={}\[\]|:;<>?\'"-]/', '', $string);
I return both the data from "echo" and after going through htmlentities() to see the difference.
when I return the data using alert(data), I get:
pr eg123 ~!##$%^&*()-+={}|[]:;< >? "...,'/...
pr eg123 ~!##$%^&*()-+={}|[]:;< >? "...,'/...
respectively. However, when I put either of those into $("#div").html(data), I get:
pr eg123 ~!##$%^&*()-+={}|[]:;< >? "...,'/...
so the multiple spaces are lost. Why does the .html() function reduce the spaces? And how can I fix this? Thanks
remove "\s+" from your regular expression and try again
"I'm trying to make sure all those characters aren't replaced." you mean it?
so i make a test like below:
$string = "~!##$%^&*()-+={}|[]:;< >?";
// $string = preg_replace('/[^a-zA-Z0-9]/', '', $string);
echo "'", $string, "'";
output is
'~!##$%^&*()-+={}|[]:;< >?'
if you want keep whole white space between "<" and ">" in $string, i can say that no way, if you wanna output the same white spaces as you input: there are two way can make this:
1> use <pre> tage
2> use replace the white space
does you want these? if you want to keep all? why use regular?
Related
Let's say I have a variable $foo:
$foo = " a a blablabla a a";
But when I do var_dump($foo) the following gets outputted:
string(57) " a a blablabla a a"
It's like the length (57 in this case) it's correct and it counts the spaces, but it doesn't display them.
How can I display the full string, including the multiple spaces in between the other characters?
If you use <pre> tag the text appears in the brower as you typed it. Couple of links:
http://www.tizag.com/htmlT/htmlpre.php
http://www.htmlcodetutorial.com/linepar/_PRE.html
You could also replace spaces with non breaking space .
Web browsers ignore a lot white space in the code which is nice. Otherwise we wouldn't be able to intend our source code or use newlines much...
Are you by any chance doing this in a browser. If so you need to do this.
<pre>
<?php echo var_dump($foo); ?>
</pre>
Because I just tried in command line and I get the output with spaces. Browsers don't handle multiple spaces and they trim them down. If you want a browser to handle multiple spaces you have to use the output inside a <pre> tag or use instead of spaces.
something like echo str_replace(' ', ' ',$test);
I'm working on transferring data from one database to another. For this I have to map some values (string) to integers and this is where I run into a strange problem.
The string looks like this $string = "word anotherword"; so two words (or one space).
When I explode the string or count the amount of spaces it misses the white space. Why? I var_dumped the variable and it says it's a string.
Below is the code i'm using.
echo "<strong>Phases</strong>: ".$fases = mapPhase($lijst[DB_PREFIX.'projectPhase']);
The string that's being send to the function is for example "Design Concept". This calls the following function (where the spaces get ignored)
function mapPhase($phases){
echo "Whitespace amount: ".substr_count($phases, ' ')."<br />";
}
For the example string given this function echoes 0. What's causing this and how can i fix it? The strangest thing is that for one instance the function worked perfectly.
More than one whitespaces (in HTML) are always converter into one whitespace. For example code indents.
If you want to print more than one, one by one use &nbps; instead.
function mapPhase($phases){
echo 'Whitespace amount: '.substr_count($phases, ' ').'<br />';
}
It may well be that the alleged space in the string may not be a space as in ' ', but something similar, which gets rendered in the browser in the same way as ' ' would. (for a rudimentary list of possible characters: http://php.net/manual/en/function.trim.php)
Thus, checking what the whitespace exactly is may be the solution to that problem.
Maybe they are not even spaces. Try ord() for each symbol in your string.
ord(' ') is 32.
You can use:
$string = preg_replace('/\s+/', '', $string);
when I view source on my php page I get " for a quote. But instead, I would like " to be used in the source code. I have no control over manually replacing it so Im wondering if there is a function to do such a thing.
If you have access to the PHP and want to change all html special characters to their rightful variations use:
print htmlspecialchars_decode($string);
You could do this very simply using str_replace.
$string = str_replace('"', '"', $string);
However, as Levi said, why not just leave it this way? It should have no effect on the display.
I have a feeling the answer is "it's not possible," but thought I'd ask to satisfy my curiosity.
I have some code that's echoed where the \n is unavoidable:
echo "Hello \n";
echo "World!";
I'd like the line to simply read (in the code output):
Hello World!
... thus removing the \n.
So I was wondering if it's possible to execute a "backspace" character during PHP's output?
Something simple like str_replace( "\n", 'backspace-character', $str );
Yes, the backspace character is ASCII character code 8 (According to the ASCII table), so you can output it in php using chr(). eg:
echo 'ab' . chr(8);
will output "a"
If the output target is HTML then extra spaces don't matter - browsers don't render multiple, contiguous spaces (which is why we have )
If the output target is something else, then you can simply cleanup the output. As you can see from other replies, there are a myriad of ways to do this. It seems like you're working with echo statements so the output-buffering functions will be the route you want to take.
ob_start();
echo "Hello \n";
echo "World!";
$output = preg_replace( "/ +/", ' ', str_replace( "\n", ' ', ob_get_clean() ) );
echo $output;
If you wanted to be able to do it for anything you could use the output buffer:
ob_start();
echo "Hello\n World";
$out = ob_get_contents();
ob_end_clear();
echo str_replace('\n', '', $out);
You could even use httaccess to append scripts containing this to any script called.
However, couldn't you just deal with it before it is set to stdout? Like
function print2($str){
echo str_replace("\n", '', $str);
}
This is not a direct answer to his exact question, but to what the title seems to allude to: outputting a PHP "backspace" character, which is probably only useful when using the PHP CLI.
You can find the right ASCII code for this (and other characters) on the ASCII table, and then use either chr() or an escape sequence:
echo chr(8);
echo "\010";
What about just replacing the "\n" by a white space (or just nothing, if you already have one space in your incoming string) ?
Like this, for instance :
$str = "Hello\nWorld!";
var_dump($str);
$str = str_replace("\n", ' ', $str);
var_dump($str);
The first output gives :
string 'Hello
World!' (length=12)
And the second one :
string 'HelloWorld!' (length=11)
Is that not enough ?
(Or maybe I don't understand the question well)
Can't you do it like this:
$str = str_replace("\n", ' ', $str);
while (strpos($str, ' ') !== false) // while there's two spaces in a row
$str = str_replace(' ', ' ', $str);
Now $str will have every spaces or \n characters sequences replaced by only one space.
(because if you just remove \n you migth have some place where a space is missing, and if you just replace it by a space you'll have some places with multiple spaces in a row).
EDIT: i don't know if the loop is really necessary but i don't have anything to test here if str_replace will automatically do the trick (and i don't think using regexp for such a simple thing is really a good idea).
Instead of thinking about how to do a backspace character, I would suggest rethinking the problem. Why do you want to take back some of your output? Probably because you outputted it too early.
Instead of doing an echo on the intermediary values, append them to the buffer. Then edit the buffer. Then print it out. If you can't control whether the output is produced or not (for example, you're using external vendor's library that outputs stuff), use PHP output buffering.
I don't know how to do a backspace, but if you are just trying to do a new line I would use:
echo "<br>";
PHP code allows for html code as long as it is used in a print or echo statement and is inside double quotes.
I have tried and tried and tried now.
I have a mysql field (TEXT) which contains the value of a textarea.
That is, if the text is written EXACTLY like this:
Hello. Hello.
Hello.
Hel
lo.
Then thats how it will appear in the mysql field because Im using wordwrap and nl2br functions when inserting it there.
Now, I am creating a dynamic page, where the 'meta description content' is updated with the 'TEXT' content, BUT, in one long string without any breaks and new lines.
That said, I need to make text to be in one string.
I have used this:
str_replace ("<br/>", "", $string);
This displays the text as one string, yes, but when viewing the source code of the page, you can see that the breaks are there, like this:
<meta name="description" content="
Hello. Hello.
Hello.
Hel
lo." />
I have also tried replacing the with '\n' and others, without success...
How can I solve this irritating problem?
Let me know if you need more input, and I will update this Q.
Replace all groups of whitespace with a single space:
$output = preg_replace('!\s+!', ' ', $input);
Try replacing "\n" with "" instead.
handy function:
function removeEmptyLines($s) {
return preg_replace("/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/", "\n", $s);
}
You might try to something like this str_replace("\n\r", "", $string); I have found that sometimes the return character \r is hiding in there someplace.