Jump to content

Preg_Split() Multiple Delimiter ?


natasha_thomas

Recommended Posts

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

Link to comment
Share on other sites

 

    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?

 

Link to comment
Share on other sites

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.

 

 

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.  8) 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;

}

 

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.