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?

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...

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?

@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.)

commented: ah, much bettter +8

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

Member Avatar for diafol

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.

@Diafol...

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

Michael

@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.

Member Avatar for diafol

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

Sorry missed Atlis mention of it :) Touche my friend, touche.

M

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.