<?php
function get_chapter( $text , $terms ) {
if ( empty ( $text ) ) return ;
$values = false ;
//make e.g. "chapters" match either "c" OR "Chapters"
foreach ( $terms as $term )
//revert this to your previous one if you want the "terms" provided explicitly
$matcher = '/((' . implode ( '|' , $terms_quoted ) . ')\s*(\d+(?:\s*[&+,.-]*\s*?)*)+)+/i' ;
//match the "chapter" expressions you provided
if ( ! empty ( $matches [ 0 ] ) ) {
//extract the numbers, in order, paying attention to existing hyphen/range identifiers
$bot = NULL ;
$top = NULL ;
$nextIsTop = false ;
$setv = function ( & $b ,& $t , $v ) { $b = $v ; $t = $v ; } ;
$flatten = function ( & $b ,& $t , $n ,& $r ) { $x = $b ; if ( $b != $t ) $x = $x . '-' . $t ; array_push ( $r , $x ) ; $b = $n ; $t = $n ; return $r ; } ; foreach ( $numbers [ 0 ] as $num ) {
if ( $num == '-' ) $nextIsTop = true ;
elseif ( $nextIsTop ) {
$top = $num ;
$nextIsTop = false ;
}
elseif ( is_null ( $bot ) ) $setv ( $bot , $top , $num ) ; elseif ( $num - $top > 1 ) $flatten ( $bot , $top , $num , $results ) ;
else $top = $num ;
}
return implode ( ' & ' , $flatten ( $bot , $top , $num , $results ) ) ; }
}
}
}
$text = array ( '9 text chapter 25.6 text' , // c25.6 'text chapter 25.6 text' , // c25.6
'text chapters 23, 24, 25 text' , // c23-25
'chapters 23+24+25 text' , // c23-25
'chapter 23, 25 text' , // c23 & 25
'text chapter 23 & 24 & 25 text' , // c23-25
'text c25.5-30 text' , // c25.5-30
'text c99-c102 text' , // c99-102
'text chapter 99 - chapter 102 text' , // c99-102
'text chapter 1 - 3 text' , // c1-3
'33 text chapter 1, 2 text 3' , // c1-2
'text v2c5-10 text' , // c5-10
'text chapters 23, 24, 25, 29, 31, 32 text' , // c23-25 & 29 & 31-32
) ;
$terms = array ( 'chapter' , 'chapters' ) ; foreach ( $text as $snippet )
{
$chapter = get_chapter( $snippet , $terms ) ;
print ( "Chapter is: c" . $chapter . "\n " ) ;
//print_r($chapter);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F9hbGwoJy9cZCsoPzpcLlxkKyk/fC0rLycsICRtYXRjaGVzWzBdLCAkbnVtYmVycykpIHsKICAgICAgICAgICAgICAgICRib3QgPSBOVUxMOwogICAgICAgICAgICAgICAgJHRvcCA9IE5VTEw7CiAgICAgICAgICAgICAgICAkbmV4dElzVG9wID0gZmFsc2U7CiAgICAgICAgICAgICAgICAkcmVzdWx0cyA9IGFycmF5KCk7CiAgICAgICAgICAgICAgICAkc2V0diA9IGZ1bmN0aW9uKCYkYiwmJHQsJHYpeyRiPSR2OyR0PSR2O307CiAgICAgICAgICAgICAgICAkZmxhdHRlbiA9IGZ1bmN0aW9uKCYkYiwmJHQsJG4sJiRyKXskeD0kYjtpZigkYiE9JHQpJHg9JHguJy0nLiR0O2FycmF5X3B1c2goJHIsJHgpOyRiPSRuOyR0PSRuO3JldHVybiRyO307CiAgICAgICAgICAgICAgICBmb3JlYWNoICgkbnVtYmVyc1swXSBhcyAkbnVtKSB7CiAgICAgICAgICAgICAgICAJaWYgKCRudW0gPT0gJy0nKSAkbmV4dElzVG9wID0gdHJ1ZTsKICAgICAgICAgICAgICAgIAllbHNlaWYgKCRuZXh0SXNUb3ApIHsKCQkJCQkJJHRvcCA9ICRudW07CgkJCQkJCSRuZXh0SXNUb3AgPSBmYWxzZTsKICAgICAgICAgICAgICAgIAl9CiAgICAgICAgICAgICAgICAJZWxzZWlmIChpc19udWxsKCRib3QpKSAkc2V0digkYm90LCR0b3AsJG51bSk7CiAgICAgICAgICAgICAgICAJZWxzZWlmICgkbnVtIC0gJHRvcCA+IDEpICRmbGF0dGVuKCRib3QsJHRvcCwkbnVtLCRyZXN1bHRzKTsKICAgICAgICAgICAgICAgIAllbHNlICR0b3AgPSAkbnVtOwogICAgICAgICAgICAgICAgfQoJCQkJcmV0dXJuIGltcGxvZGUoJyAmICcsICRmbGF0dGVuICgkYm90LCR0b3AsJG51bSwkcmVzdWx0cykpOwogICAgICAgICAgICB9CiAgICAgICAgfQogICAgfQp9CgoKJHR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