php - How can i count the number of words between two words? -


How do I count the number of words between two words?

$ txt = "tükörfúrógép banana orange lime, tükörfúrógép cherry árvíztűrő orange lion cat limestone mac tükörfúrógép cat orange lime cat árvíztűrő tükörfúrógép banana orange lime orange lime cat árvíztűrő";

Two words: 'árvíztűrő' and 'tükörfúrógép'
I need this back:
tükörfúrógép cherry árvíztűrő
tükörfúrógép cat orange lime cat árvíztűrő
tükörfúrógép banana orange lime orange lime cat árvíztűrő

now I have this regular expression:

 < code> preg_match_all ( '@ ((tükörfúrógép (*) árvíztűrő) (árvíztűrő (*) tükörfúrógép)) @ needle.?.?', $ txt, $ M);  

I have several things to indicate:

    < Li> You can not do it in a Reggae Regex Forward-Only, Reversed Mail Order requires a second regex.
  1. You (. *)? , but you mean (. *)?
  2. To get the right match, you should make sure that the left border of your expression can not be in the middle.
  3. You should denote the word Edit: This is true in theory, but it does not work for Unicode input in PHP.
  4. You should change the Hungarian PHP location (this is is is Hungary, is not it?) Before calling preg_match_all () , Because the locale is considered to be a word boundary on PHP but there is an effect. Edit: \ b actually changes with not selected. The locale

is being said, # 1 is regex:

 (\ btükörfúrógép \ b) ((: (1)?!.) *?) \ Bárvíztűrő \ b 

and

regex analog # 2., just the opposite delimiter words

Regex explanation:

  (# match group 1: \ b # a word boundary # first delimiter word \ b # tükörfúrógép a word limit) # end match group 1 (# match group 2: (?: # non-capturing group: (?! # look-FW : # 1 should not be made after the delimiter word 1) #End-look-ahead # any next four matches (with \ "switch" \ n included A)) * # Repeat end non-capturing group, necessary as often) # end match Group 2 (this is what looks to you) \ b # a word limit árvíztűrő # Your second Delimiter word \ b # Update a word boundary  <:  Php's  Pitiable  with bad Unicode string support, you use replacements for expressions such as  \ b Will be compelled to : 

  $ first = '(? & Lt; = ^ | [^ \ P {L}] ';'; After $ = '(? = [^ \ P {L}] | $)';  

This suggestion has been taken from.


Comments