Asked  2 Years ago    Answers:  5   Viewed   63 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

2

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!'
);
Saturday, September 24, 2022
5

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

Wednesday, August 10, 2022
 
massa
 
3

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.

Friday, December 16, 2022
 
bhushan
 
2

You really should implement prefersStatusBarHidden on your view controller(s):

Swift 3 and later

override var prefersStatusBarHidden: Bool {
    return true
}
Wednesday, September 21, 2022
 
2

Assuming you are using a ModelForm, it's actually fairly simple. The task is something like this: retrieve the object of the model that you want to populate your 'edit' for with, create a new form based on your ModelForm, and populate it with the object using 'instance'.

Here's the skeleton of your view:

def view(request): 
  obj = Model.objects.get(pk = objectpk)
  form = MyModelForm(instance = obj)

  return render (request, "template", {'form' = form})

You can access the 'initial' values by using something like:

form.fields['fieldname'].initial = somevalue

And then you'd return the form like above.

Thursday, October 13, 2022
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 :
 

Browse Other Code Languages