Viewed   121 times

"something here ; and there, oh,that's all!"

I want to split it by ; and ,

so after processing should get:

something here

and there

oh

that's all!

 Answers

1
<?php

$pattern = '/[;,]/';

$string = "something here ; and there, oh,that's all!";

echo '<pre>', print_r( preg_split( $pattern, $string ), 1 ), '</pre>';

Updated answer to an updated question:

<?php

$pattern = '/[x{ff0c},]/u';

//$string = "something here ; and there, oh,that's all!";
$string = 'hei,nihao?a ';


echo '<pre>', print_r( preg_split( $pattern, $string ), 1 ), '</pre>';
Friday, November 25, 2022
4

Try:

$arr=preg_split("/s+(?=S*+$)/",$str);

Edit

A short explanation:

The (?= ... ) is called a positive look ahead. For example, a(?=b) will only match a single 'a' if the next character (the one to the right of it) is a 'b'. Note that the 'b' is not a part of the match!

The S is just a short-hand for the character class [^s]. In other words: it matches a single character other than a white space character. The + after the * makes the character class S possessive.

Finally, the $ denotes the end of the string.

To recap, the complete regex s+(?=S*+$) would read in plain English as follows:

match one or more white space characters only when looking ahead of those white space characters zero or more characters other than white space characters, followed by the end of the string, can be seen.

Saturday, September 10, 2022
 
5

PHP

$str = "a,b c,d;e f";

$pieces = preg_split('/[, ;]/', $str);

var_dump($pieces);

CodePad.

Output

array(6) {
  [0]=>
  string(1) "a"
  [1]=>
  string(1) "b"
  [2]=>
  string(1) "c"
  [3]=>
  string(1) "d"
  [4]=>
  string(1) "e"
  [5]=>
  string(1) "f"
}
Tuesday, December 13, 2022
 
5

correct me if i'm wrong, but i don't think you can do this with a simple regexp. in a full regexp implementation you could use something like this :

$parts = preg_split("/(?<!<[^>]*)./", $input);

but php does not allow non-fixed-length lookbehind, so that won't work. apparently the only 2 that do are jgsoft and the .net regexp. Useful Page

my method of dealing with this would be :

function splitStringUp($input, $maxlen) {
    $parts = explode(".", $input);
    $i = 0;
    while ($i < count($parts)) {
        if (preg_match("/<[^>]*$/", $parts[$i])) {
            array_splice($parts, $i, 2, $parts[$i] . "." . $parts[$i+1]);
        } else {
            if ($i < (count($parts) - 1) && strlen($parts[$i] . "." . $parts[$i+1]) < $maxlen) {
                array_splice($parts, $i, 2, $parts[$i] . "." . $parts[$i+1]);
            } else {
                $i++;
            }
        }
    }
    return $parts;
}

you didn't mention what you want to happen when an individual sentence is >8000 chars long, so this just leaves them intact.

sample output :

splitStringUp("this is a sentence. this is another sentence. this is an html <a href="a.b.c">tag. and the closing tag</a>. hooray", 8000);
array(1) {
  [0]=> string(114) "this is a sentence. this is another sentence. this is an html <a href="a.b.c">tag. and the closing tag</a>. hooray"
}

splitStringUp("this is a sentence. this is another sentence. this is an html <a href="a.b.c">tag. and the closing tag</a>. hooray", 80);
array(2) {
  [0]=> string(81) "this is a sentence. this is another sentence. this is an html <a href="a.b.c">tag"
  [1]=> string(32) " and the closing tag</a>. hooray"
}

splitStringUp("this is a sentence. this is another sentence. this is an html <a href="a.b.c">tag. and the closing tag</a>. hooray", 40);
array(4) {
  [0]=> string(18) "this is a sentence"
  [1]=> string(25) " this is another sentence"
  [2]=> string(36) " this is an html <a href="a.b.c">tag"
  [3]=> string(32) " and the closing tag</a>. hooray"
}

splitStringUp("this is a sentence. this is another sentence. this is an html <a href="a.b.c">tag. and the closing tag</a>. hooray", 0);
array(5) {
  [0]=> string(18) "this is a sentence"
  [1]=> string(25) " this is another sentence"
  [2]=> string(36) " this is an html <a href="a.b.c">tag"
  [3]=> string(24) " and the closing tag</a>"
  [4]=> string(7) " hooray"
}
Thursday, December 15, 2022
 
dvlsg
 
4

You could first do a Replace on the string first and then do the split:

newString = Replace(origString, "-", " ")
newArray = Split(newString, " ")
Friday, November 25, 2022
 
molow
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :