|
tommytx
|
 |
« on: April 09, 2009, 02:11:19 PM » |
|
I know this is really a simple command as i came across it just the other day.. I searched for regular expressions one line but nothing showed up. It had something where it just used .?! any one of these followed by a spaced and simply inserted a line feed.
Can someone help me on this?
|
|
|
|
|
Logged
|
|
|
|
|
perkiset
|
 |
« Reply #1 on: April 09, 2009, 02:25:16 PM » |
|
um, what language are you in? what OS? Doing this from the shell?
in PHP, if you wanted to change chr(13) or chr(10) into just a space to concatenate a bunch of CRLF / LF sentences into one big line you could do it much more quickly with a str replace:
$newBlock = str_replace(array(chr(10) . chr(13), chr(13), chr(10), ' ', ' ', $inputBuff);
This would change chr(10) + chr(13), chr(13), chr(10) or 2 sequential spaces into a single space.
|
|
|
|
|
Logged
|
It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
|
|
|
|
tommytx
|
 |
« Reply #2 on: April 09, 2009, 03:56:44 PM » |
|
Thanks Perkiset, that looks good... By the way I am doing php. Windows XP Pro. Actually on Xampp but that should not matter..
But here is the problem with the code you sent... it looks for existing 10 and 13... What I need is the old time honored convert every sentence no matter how long or short to a single line entry. For example:
Spot is my dog. See spot run. Spot can run faster than any other dog on the planet. Now if you want to see a fast dog let me show you Rambo. Rambo can run very fast. You would not believe his speed! Wow! and that is an understatement. Do you have any fast dogs?
You see I start out with only 2 lines and one return or 2 line feed. ************************************************* What I need is : Spot is my dog. See spot run. Spot can run faster than any other dog on the planet. Now if you want to see a fast dog let me show you Rambo. Rambo can run very fast. You would not believe his speed! Wow! and that is an understatement. Do you have any fast dogs?
Now I really didn't want to split the Wow! but its ok... as the exclamation will almost never occur as will the question mark.. very very infrequently.. so it really won't matter. But I think I need to look for a space following the punctuation also to keep it from finding such as mysite.com or $345.98 as a period. Don't want to use a punctuation followed by a double space as its scanning html and many times the double space for a new sentence is not there.
And the only way I can see to do this is detect all periods, question marks and exclamation marks to start a new line.
Something like: ************ Meaning find character followed by a space
$find = ". " or "? " or "! "
$replace = "return or new line"
|
|
|
|
|
Logged
|
|
|
|
|
tommytx
|
 |
« Reply #3 on: April 09, 2009, 05:19:18 PM » |
|
Perk.. are you aware the Rss for SEOIDIOTS is broken link? feed://www.perkiset.org/forum/index.php?type=rss;action=.xml just in case you are not aware..
|
|
|
|
|
Logged
|
|
|
|
|
vsloathe
|
 |
« Reply #4 on: April 09, 2009, 07:27:13 PM » |
|
Banged it out in this window, see if it works for you: <?php $content = 'READ YOUR TEXT IN OR C+P HERE'; $buff = array(); $buff = preg_split('/[.|!|?]/', $content); foreach($buff as $sentence){ echo $sentence.".\n"; } ?>
|
|
|
|
|
Logged
|
hai
|
|
|
|
Bompa
|
 |
« Reply #5 on: April 09, 2009, 08:14:42 PM » |
|
Banged it out in this window, see if it works for you: <?php $content = 'READ YOUR TEXT IN OR C+P HERE'; $buff = array(); $buff = preg_split('/[.|!|?]/', $content); foreach($buff as $sentence){ echo $sentence.".\n"; } ?>
This thread should be "How to Parse Sentances from a large paragraph".  I've done it like VS's code, (but i dont use | inside square brackets), long ago when injecting stuff into html paragraphs.
|
|
|
|
|
Logged
|
"Everything that can be counted does not necessarily count; everything that counts cannot necessarily be counted." -- Albert Einstein
|
|
|
|
tommytx
|
 |
« Reply #6 on: April 09, 2009, 08:17:58 PM » |
|
Thank you very much.. just exactly what I was looking for.. I came across it the other day but could not find it again. I will put a copy of this snippet in safe keeping for use again at a later time... PERFECT... Sorry I don't know how to mark it code to make it in a box. Here is my final i just added \r along with the \n to make it line feed. Also included final project so if any noobie was following along how to make the final usable and savable. I added the " " to make it view good on the screen as the line feeds don't work on the echo.. <?php $file = fopen("content_one_line.txt", "w"); $content = file_get_contents("mycontent.txt"); $buff = array(); $buff = preg_split('/[.|!|?]/', $content); foreach($buff as $sentence){ $sentence = trim($sentence); echo $sentence.".<br>\r\n"; echo fwrite($file, $sentence . ".\r\n"); } fclose($file); ?>
|
|
|
|
« Last Edit: April 09, 2009, 08:35:02 PM by Bompa »
|
Logged
|
|
|
|
|
tommytx
|
 |
« Reply #7 on: April 09, 2009, 08:23:36 PM » |
|
Just in case anyone is asking what the hell is this with the period included..-> .\r\n Because the sucker ate the punctuation, I just added the period back at the end of the line.. Actually the proper way to do it would be to capture the punctuation mark like (?) or (.) and add that particular mark at the end of the screen... I know it can be done, but you have helped so much i didn't have the heart to ask how.. and anyway a plain period will be the mark in 98 per cent of the cases anyway...
Thanks again..
|
|
|
|
|
Logged
|
|
|
|
|
tommytx
|
 |
« Reply #8 on: April 09, 2009, 08:26:48 PM » |
|
Bompa great idea for a subject, I just didnt think of it.. Its nice when everyone uses the very best title so when searching it comes up more frequently.. I used the subject above... don't know if that becomes a search item or not..
|
|
|
|
|
Logged
|
|
|
|
|
nop_90
|
 |
« Reply #9 on: April 09, 2009, 08:38:12 PM » |
|
" ".join("a\nb\nc\n".split("\n"))
|
|
|
|
|
Logged
|
|
|
|
|
Bompa
|
 |
« Reply #10 on: April 09, 2009, 08:40:59 PM » |
|
Cool. You could put parenthesises around the square brackets. Then whatever is "captured" by the regex will be in $1. preg_split('/([.!?])/', $content); $puncuation = $1; foreach($buff as $sentence){ $sentence = trim($sentence); echo $sentence . "$puncuation <br>\r\n"; }
Bompa
|
|
|
|
|
Logged
|
"Everything that can be counted does not necessarily count; everything that counts cannot necessarily be counted." -- Albert Einstein
|
|
|
|
tommytx
|
 |
« Reply #11 on: April 09, 2009, 09:26:15 PM » |
|
EDIT: BAH! Sorry tommy. I went to hit "quote" to quote your post but I hit modify instead!  Sorry! -V Well, that's because Bompa doesn't write PHP  <?php $content = "See my dog run. Can your dog run fast? Hell yes he can!"; $buff = array(); $buff = preg_split('/([.!?])/', $content, -1, PREG_SPLIT_DELIM_CAPTURE); foreach($buff as $sentence){ $sentence = trim($sentence); echo $sentence . "<br>\r\n"; echo fwrite($file, $sentence . "\r\n"); }
Now it should just retain the original punctuation mark. Or it might put it as a second dimension on the array, I'm not sure. Test it and let me know.
|
|
|
|
« Last Edit: April 10, 2009, 06:33:44 AM by vsloathe »
|
Logged
|
|
|
|
|
perkiset
|
 |
« Reply #12 on: April 09, 2009, 10:10:16 PM » |
|
 How to break a ton of sentences into one single line with line feed.
break, INTO one single line with line feed... I read remove 13/10s and put into one big buff. Sorry meng.
|
|
|
|
|
Logged
|
It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
|
|
|
|
vsloathe
|
 |
« Reply #13 on: April 10, 2009, 06:37:43 AM » |
|
here: <?php $content = "See my dog run. Can your dog run fast? Hell yes he can!"; $buff = array(); $buff = preg_split('/([.!?])/', $content, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY); foreach($buff as $key => $sentence){ if($sentence != '.' && $sentence != '?' && $sentence != '!'){ $sentence = trim($sentence); echo $sentence . $buff[$key + 1] . "\n"; } }
|
|
|
|
|
Logged
|
hai
|
|
|
|
vsloathe
|
 |
« Reply #14 on: April 10, 2009, 06:40:24 AM » |
|
By the way, this has been quite helpful. I'd never thought of capturing the actual punctuation mark.
|
|
|
|
|
Logged
|
hai
|
|
|
|