fork(1) download
  1. <?php
  2. function get_chapter($text, $terms) {
  3.  
  4. if (empty($text)) return;
  5. if (empty($terms) || !is_array($terms)) return;
  6.  
  7. $values = false;
  8.  
  9. $terms_quoted = array();
  10. foreach ($terms as $term)
  11. $terms_quoted[] = preg_quote($term, '/');
  12.  
  13. // search for matches in $text
  14. // matches with lowercase, and ignores white spaces...
  15. if (preg_match('/('.implode('|', $terms_quoted).')\s*(\d+(\.\d+)?)/i', $text, $matches)) {
  16. if (!empty($matches[2]) && is_numeric($matches[2])) {
  17. $values = array(
  18. 'term' => $matches[1],
  19. 'value' => $matches[2]
  20. );
  21. }
  22. }
  23.  
  24. return $values;
  25. }
  26.  
  27.  
  28. $text = '9 text chapter 25.6 text'; // c25.6
  29. $terms = array('chapter', 'chapters');
  30. $chapter = get_chapter($text, $terms);
  31.  
  32. print_r($chapter);
  33.  
  34. if ($chapter) {
  35. echo 'Chapter is: c'. $chapter['value'];
  36. }
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
Success #stdin #stdout 0.02s 23668KB
stdin
Standard input is empty
stdout
Array
(
    [term] => chapter
    [value] => 25.6
)
Chapter is: c25.6