Msg#: 4415239 posted 1:11 am on Feb 8, 2012 (gmt 0)
For indenting a string of text coming from a textarea I'm using the following regex:
$string =~ s/\x20/ \;/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/ \;/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.
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 <p>blahblah</p> <p>blahblah</p> instead of blahblah<br> blahblah<br>
Paragraphs would make the whole problem go away, because you could then throw in a line or two of CSS to indent them all.
Msg#: 4415239 posted 6:18 am on Feb 8, 2012 (gmt 0)
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.
Msg#: 4415239 posted 9:58 am on Feb 8, 2012 (gmt 0)
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.
Msg#: 4415239 posted 10:03 am on Feb 8, 2012 (gmt 0)
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".
Msg#: 4415239 posted 3:19 pm on Feb 12, 2012 (gmt 0)
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.