Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k & phranque

Message Too Old, No Replies

Space Regex

regex for indent



1:11 am on Feb 8, 2012 (gmt 0)

5+ Year Member

For indenting a string of text coming from a textarea I'm using the following regex:

$string =~ s/\x20/&nbsp\;/g;

The problem with this is that   is between every word when the string is inserted into the db/displayed on a web page. In effect it is working as planned but I see it causing extra loading time for the web page and putting a ton of  's in the db.

Is there any way the regex can be modified to just "do it's thing" at the beginning of a string and not everywhere it finds an empty space?

I tried $string =~ s/^\x20/&nbsp\;/g; but when I did that I could only indent by one space and only one the first paragraph. While I did manage to figure out how to separate the paragraphs by using a regex to insert a <br /> tag I haven't been able to come up with anything on this one yet.

I could live without the indentations but it has become a personal thing and some people just like to have their paragraphs started with an indent.



2:12 am on Feb 8, 2012 (gmt 0)

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month

You're going to need to add the language-specific parts yourself, but from the RegEx side:

You have to capture the entire text block in one go, so you can change

^blahblah blahblah blahblah$


&nbsp;&nbsp;&nbsp;&nbsp;blahblah blahblah blahblah

instead of treating each word as a separate unit.

If a single text block contains multiple paragraphs-- as your reference to <br /> implies-- then you've actually done the work already. Don't just insert a break: insert a break and a group of spaces at the same time.

I assume there's some compelling reason why you can't use
instead of

Paragraphs would make the whole problem go away, because you could then throw in a line or two of CSS to indent them all.


6:18 am on Feb 8, 2012 (gmt 0)

5+ Year Member

Actually Lucy, you gave me a real eye opener. I see that you can actually add the style class to the <p> tag such as:
$request =~ s/\r\n/<p class=\"indent\">/g; if the class is needed and also no matter how many times they hit the spacebar the space between tparagraphs will always be the same unlike using the <br /> tag in which case if they hit the spacebar 5 times there would be 5 spaces between paragraphs where even if there are 5 <p> tags the distance between paragraphs remains at only one space. The only thing is, how do you include the closing </p> tag?

It is not that important that paragraphs are indented even though the way you described makes it possible--I was only thinking about people who like to indent paragraphs.

As far as adding css to regex---is this a rabbit you pulled out of a hat or something that has been done before---cool trick.

Thanx---gonna' have to put some thought into the direction I take on this one.

Thanx again.


9:58 am on Feb 8, 2012 (gmt 0)

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

the closing paragraph tag isn't required for validation in HTML (4 or 5) but is required in XHTML.
if the </p> is missing it will be implied by the opening tag of the next block-level element.
therefore you only need it if you want to validate for XHTML or if there isn't a new block level element where you need one.

i prefer to use the </p> tag in all cases to avoid ambiguity and future maintenance headaches.

one possible solution is to use your intended pattern for finding the space between paragraphs and replace the \r\n with </p><p class=... and then add the opening and closing paragraph tags to the beginning and end of the entire text block.


10:03 am on Feb 8, 2012 (gmt 0)

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month

Weird but true: If you don't care about it validating, you don't need a closing </p> tag in html4. But if you have multiple <p> statements there will be multiple empty lines.

If you can put <br> in your output then you can put <p> or <p class = "long complicated list of conditions"> or <a href = "worldsbestsite.com"> or anything else you want ;)

Now, as already noted, I don't speak the language, but if you can identify spaces in the text area can't you identify line breaks? Trim away any leading or trailing ones. Then you have <p> at the beginning, </p>\r\n<p> replacing each set of (\r\n)+ along the way, and a final </p> at the end.

Does the replacement just run once, or can you make a "while" type of iteration? You want it structured as "do such-and-such until you run out of things to do it to".


3:19 pm on Feb 12, 2012 (gmt 0)

5+ Year Member

This started out as a "quest" of sorts and learned some things also which is good but I'm now convinced that the way to go is replacing the <textarea> with something like FCK editor and let it take care of everything. The indent was not really as important as the newlines.

Featured Threads

Hot Threads This Week

Hot Threads This Month