natasha_thomas Posted January 17, 2010 Share Posted January 17, 2010 Friends, I want to use Multiple delimiters to form an Arrar from a text. delimiter i want to use are: 1- "." followed by a Space 2- "?" Followed by a Space 3- "." followed by a new Line "\n" Code i am using is $arr = preg_split("/(\.\s+)/", $arr); This only solving delimiter 1. how to achieve all the there Delimiters? Thanks all Quote Link to comment https://forums.phpfreaks.com/topic/188765-preg_split-multiple-delimiter/ Share on other sites More sharing options...
redarrow Posted January 17, 2010 Share Posted January 17, 2010 Natasha write out examples in sentences with the added problems cheers.. Quote Link to comment https://forums.phpfreaks.com/topic/188765-preg_split-multiple-delimiter/#findComment-996513 Share on other sites More sharing options...
natasha_thomas Posted January 17, 2010 Author Share Posted January 17, 2010 Natasha write out examples in sentences with the added problems cheers.. Ok Let me try... Example: Input text This is test a. This is test b? This is test c. This Input should give three elements in Array: This is test a. This is test b? This is test c. Did i make it Clear? What shall i put in preg_sllit() function to achieve this? Quote Link to comment https://forums.phpfreaks.com/topic/188765-preg_split-multiple-delimiter/#findComment-996522 Share on other sites More sharing options...
salathe Posted January 17, 2010 Share Posted January 17, 2010 /[.?]\s+/ That will do what you want as well as a few other things like split on a ? followed by a newline and \s includes other whitespace like tab, formfeed and carriage return characters. Quote Link to comment https://forums.phpfreaks.com/topic/188765-preg_split-multiple-delimiter/#findComment-996585 Share on other sites More sharing options...
natasha_thomas Posted January 17, 2010 Author Share Posted January 17, 2010 Thanks Salathe BUT.... Now am having strange problem.... Sample Text: How are you? is coming like: How are you?. (Observe the "." after the question mark) Top of it: The Newline is also not being treated as Delimiter, which i want. I am sure this is happening because of $arr = preg_split("/[.?]\s+/", $arr); //$arr = file ( $file ); $total_lines = count( $arr ); $max_lines = min( $max_lines, $total_lines); $r = range(1, $total_lines); shuffle($r); for( $a=1; $a <= $max_lines ; $a++ ) $arr_random[] = $arr[ $r[$a-1]-1 ]; $shuffled = join( ". ", $arr_random) . "."; . may you tell, what changes we need in this Code? Natasha T. Quote Link to comment https://forums.phpfreaks.com/topic/188765-preg_split-multiple-delimiter/#findComment-996619 Share on other sites More sharing options...
cags Posted January 17, 2010 Share Posted January 17, 2010 @natasha_thomas: Try giving us a complete sample, at least a paragraph or so, along with what you'd expect it to be separated out to (just list east part on a separate line. Example: Input: The quick brown fox jumps over the lazy dog? The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. Result: The quick brown fox jumps over the lazy dog? The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. Quote Link to comment https://forums.phpfreaks.com/topic/188765-preg_split-multiple-delimiter/#findComment-996628 Share on other sites More sharing options...
natasha_thomas Posted January 17, 2010 Author Share Posted January 17, 2010 Input: The quick brown fox jumps over the lazy dog? The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. Result: The quick brown fox jumps over the lazy dog? The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. Is what am looking for. What should be changed in my Existing code that i can achieve it? My Existing Code: <?php $file = $_GET['file']; $max_lines = $_GET['lines'] ; $shuffled_para = get_random_lines( $file, $max_lines ); echo $shuffled_para; function get_random_lines( $file, $max_lines ) { $arr = file_get_contents ( $file ); $arr = trim(trim($arr), "."); $arr = preg_split("/\.\s+/", $arr); //$arr = file ( $file ); $total_lines = count( $arr ); $max_lines = min( $max_lines, $total_lines); $r = range(1, $total_lines); shuffle($r); for( $a=1; $a <= $max_lines ; $a++ ) $arr_random[] = $arr[ $r[$a-1]-1 ]; $shuffled = join( ". ", $arr_random) . "."; return $shuffled; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/188765-preg_split-multiple-delimiter/#findComment-996635 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.