0

How can I replace <br>, \r, \n with " ". Which is an empty space. Is there a way to detect these characters in a string and replace them with a single white space?

6
Contributors
10
Replies
48
Views
4 Years
Discussion Span
Last Post by mmcdonald
-2

Using what? Do you want to do this in "vi"? Programatically? In Ruby? In C++? in Perl? Need some more information before we can answer...

0

If I recall (i am sorry if i am wrong) but you can use &thinsp; to create a space (as far as i am aware of, this works in html to create a single space).

You are asking for a single space right?

0

@sparker, I think it's fair to assume he's talking about PHP here, give that he posted the question in the PHP forum.

To answer the question: in PHP, simple string replacement can be done using the str_replace() function. Note that $search and $replace parameters (first and second, respectively) for the function can take arrays, so if you wanted to replace all of the three items you mentioned, you could put them in an array and pass that as the first parameter, and then pass the space character as the second parameter.

If a more complicated replacement is needed, PHP's regular expression replacement function, preg_replace(), can be used instead.

If I recall (i am sorry if i am wrong) but you can use &thinsp; to create a space (as far as i am aware of, this works in html to create a single space).

The &thinsp; entity should indeed create a space, but a "thin" space, which is shorter than a normal space. How short would depend on the browser, but it could be significantly shorter.

A standard space can be added in HTML using the &nbsp; entity. I'd recommend that instead, since its' more or less "the" space entity in HTML, supported in every browser ever made. (Support for &thinsp; isn't as widespread.)

Votes + Comments
ah, much bettter
0

The &thinsp; entity should indeed create a space, but a "thin" space, which is shorter than a normal space. How short would depend on the browser, but it could be significantly shorter.

A standard space can be added in HTML using the &nbsp; entity. I'd recommend that instead, since its' more or less "the" space entity in HTML, supported in every browser ever made. (Support for &thinsp; isn't as widespread.)

Ah, now i remember &nbsp;... i am surprised how it slipped past my mind... espicially when i was using it an hour prior to responding to this thread the first time :D

Edited by <M/>

0

preg_* functions tend to slower than the 'straight' str_* functions in my experience, so if you can avoid regex do so. Remember that str_replace() can take arrays as parameters which will help avoid having to make repeated calls to this function.

0

@Diafol...

Am I going mad? :) Didn't I say str_*? Or are you just clarifying the solution? haha

Michael

Edited by mmcdonald

0

@mmcdonald, I mentioned the preg_replace functions in my post, when I suggested the str_replace function, so I'm guessing his post was to elaborate on that.

And I agree with him. Regular expressions tend to be slower than simple string replacement. They are more complex for PHP to execute.

That's not to say simple replacements happen "instantly". It does take PHP a few microseconds to work that all out.

0

For the record, I was just expanding a little (possibly needlessly - but there again I am a post whore ;) ) on Atli's post.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.