Asked  7 Months ago    Answers:  5   Viewed   30 times

I have this simple pattern that splits a text into periods:

$text = preg_split("/[.:!?]+/", $text);

But I want to include . : or ! at the end of the array items.

That is, now for "good:news.everyone!" I have:

array("good", "news", "everyone", "");

But I want:

array("good:", "news.", "everyone!", "");

 Answers

10

Here you go:

preg_split('/([^.:!?]+[.:!?]+)/', 'good:news.everyone!', -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);

How it works: The pattern actually turns everything into a delimiter. Then, to include these delimiters in the array, you can use the PREG_SPLIT_DELIM_CAPTURE constant. This will return an array like:

array (
    0 => '',
    1 => 'good:',
    2 => '',
    3 => 'news.',
    4 => '',
    5 => 'everyone!',
    6 => '',
);

To get rid of the empty values, use PREG_SPLIT_NO_EMPTY. To combine two or more of these constants, we use the bitwise | operator. The result:

array (
    0 => 'good:',
    1 => 'news.',
    2 => 'everyone!'
);
Wednesday, March 31, 2021
 
Daveel
answered 7 Months ago
66

Try this:

<?php

$string = "foo bar baz; boo, bat";

$words = preg_split('/[,.s;]+/', $string);

var_dump($words);
// -> ["foo", "bar", "baz", "boo", "bat"]

The Pattern explained

[] is a character class, a character class consists of multiple characters and matches to one of the characters which are inside the class

. matches the . Character, this does not need to be escaped inside character classes. Though this needs to be escaped when not in a character class, because . means "match any character".

s matches whitespace

; to split on the semicolon, this needs not to be escaped, because it has not special meaning.

The + at the end ensures that spaces after the split characters do not show up as matches

Saturday, May 29, 2021
 
EzzDev
answered 5 Months ago
94

I don't think using pcre regex is necessary ... if it's really splitting words you need.

You could do something like this and benchmark see if it's faster / better ...

$splitby = array('these','are','the','words','to','split','by');
$text = 'This is the string which needs to be split by the above words.';

$split = explode(' ', $text);
$result = array();
$temp = array();

foreach ($split as $s) {

    if (in_array($s, $splitby)) {
        if (sizeof($temp) > 0) {
           $result[] = implode(' ', $temp);
           $temp = array();
        }            
    } else {
        $temp[] = $s;
    }
}

if (sizeof($temp) > 0) {
    $result[] = implode(' ', $temp);
}

var_dump($result);

/* output

array(4) {
  [0]=>
  string(7) "This is"
  [1]=>
  string(18) "string which needs"
  [2]=>
  string(2) "be"
  [3]=>
  string(5) "above words."
}

The only difference with your output is the last word because "words." != "word" and it's not a split word.

Saturday, May 29, 2021
 
Eddas
answered 5 Months ago
75

You can use:

=MyFunction([Form])

The code would be:

Function MyFunction(frm As Form)
MsgBox frm.Name
End Function
Thursday, August 5, 2021
 
Grzegorz
answered 3 Months ago
49

It appears the issue was not related to my setup. The problem was that the itemValue was set to the company id property, while the comparison was being done to the company name property on my form backing bean. So the two were not equal, and therefore, no item was set to selected.

The above code works just fine, and setting the value in the userForm for a particular property will set that value as selected in select form fields so long as the value of one of the items in the items collection is equal to the form value. I changed my form field to look like this, which pulls the companyName instead of the id.

<form:form method="post" action="/admin/user/update.html" modelAttribute="userForm">
<form:select path="companyName" id="companyName" items="${vars.companyNames}" itemValue="companyName" itemLabel="companyName" />
</form:form>
Sunday, September 12, 2021
 
EEk
answered 2 Months ago
EEk
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :