fork(1) download
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  5.  
  6. <title>PhpFiddle Initial Code</title>
  7.  
  8. <script type="text/javascript">
  9. $(function (){
  10.  
  11. });
  12. </script>
  13.  
  14. <style type="text/css">
  15.  
  16. </style>
  17.  
  18. </head>
  19.  
  20. <body>
  21. <pre>
  22. <?php
  23.  
  24. function capturingSearchWithKeyword($filename, $keyword, $keepTrailing = true, $trim = false){
  25. //$lines = file($filename); No file reading on ideone. Gotta simulate.
  26.  
  27. $lines = array("Off off world clock on on on world clock off\n"
  28. , "off world on\n"
  29. , "on on on on\n"
  30. , "on world clock off\n"
  31. , "off off\n"
  32. , "off clock world on"
  33. );
  34. $keep = false;
  35. $extractions = array();
  36. $current = '';
  37. foreach($lines as $line){
  38. $parts = preg_split("/\\b$keyword\\b/i", $line);
  39. $current .= $parts[0];
  40. for ($i = 1; $i<count($parts); $i++){
  41. if ($keep) {
  42. if ($trim) $current = trim($current);
  43. $extractions[] = $current;
  44. }
  45. $keep = !$keep;
  46. $current = $parts[$i];
  47. }
  48. }
  49. if ($keep && $keepTrailing)
  50. $extractions[] = $current;
  51. return $extractions;
  52. }
  53. echo "World, default\n";
  54. var_dump(capturingSearchWithKeyword('aa', 'world'));
  55. echo "World, Keep Trailing\n";
  56. var_dump(capturingSearchWithKeyword('aa', 'world', true));
  57. echo "World, Keep Trailing, No trim\n";
  58. var_dump(capturingSearchWithKeyword('aa', 'world', true, false));
  59. echo "World, Keep Trailing, trim\n";
  60. var_dump(capturingSearchWithKeyword('aa', 'world', true, true));
  61. echo "World, Drop trailing\n";
  62. var_dump(capturingSearchWithKeyword('aa', 'world', false));
  63. echo "World, Drop trailing, no trim\n";
  64. var_dump(capturingSearchWithKeyword('aa', 'world', false, false));
  65. echo "World, Drop trailing, trim\n";
  66. var_dump(capturingSearchWithKeyword('aa', 'world', false, true));
  67. echo "Clock, default\n";
  68. var_dump(capturingSearchWithKeyword('aa', 'Clock'));
  69. echo "Clock, Keep Trailing\n";
  70. var_dump(capturingSearchWithKeyword('aa', 'Clock', true));
  71. echo "Clock, Keep Trailing, No trim\n";
  72. var_dump(capturingSearchWithKeyword('aa', 'Clock', true, false));
  73. echo "Clock, Keep Trailing, trim\n";
  74. var_dump(capturingSearchWithKeyword('aa', 'Clock', true, true));
  75. echo "Clock, Drop trailing\n";
  76. var_dump(capturingSearchWithKeyword('aa', 'Clock', false));
  77. echo "Clock, Drop trailing, no trim\n";
  78. var_dump(capturingSearchWithKeyword('aa', 'Clock', false, false));
  79. echo "Clock, Drop trailing, trim\n";
  80. var_dump(capturingSearchWithKeyword('aa', 'Clock', false, true));
  81. ?>
  82. </pre>
  83. </body>
  84. </html>
  85.  
  86.  
Success #stdin #stdout 0.01s 20568KB
stdin
Standard input is empty
stdout
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>PhpFiddle Initial Code</title>

<script type="text/javascript">
$(function (){
    
});
</script>

<style type="text/css">
	
</style>

</head>

<body>
	<pre>
World, default
array(3) {
  [0]=>
  string(16) " clock on on on "
  [1]=>
  string(19) " on
on on on on
on "
  [2]=>
  string(3) " on"
}
World, Keep Trailing
array(3) {
  [0]=>
  string(16) " clock on on on "
  [1]=>
  string(19) " on
on on on on
on "
  [2]=>
  string(3) " on"
}
World, Keep Trailing, No trim
array(3) {
  [0]=>
  string(16) " clock on on on "
  [1]=>
  string(19) " on
on on on on
on "
  [2]=>
  string(3) " on"
}
World, Keep Trailing, trim
array(3) {
  [0]=>
  string(14) "clock on on on"
  [1]=>
  string(17) "on
on on on on
on"
  [2]=>
  string(3) " on"
}
World, Drop trailing
array(2) {
  [0]=>
  string(16) " clock on on on "
  [1]=>
  string(19) " on
on on on on
on "
}
World, Drop trailing, no trim
array(2) {
  [0]=>
  string(16) " clock on on on "
  [1]=>
  string(19) " on
on on on on
on "
}
World, Drop trailing, trim
array(2) {
  [0]=>
  string(14) "clock on on on"
  [1]=>
  string(17) "on
on on on on
on"
}
Clock, default
array(2) {
  [0]=>
  string(16) " on on on world "
  [1]=>
  string(17) " off
off off
off "
}
Clock, Keep Trailing
array(2) {
  [0]=>
  string(16) " on on on world "
  [1]=>
  string(17) " off
off off
off "
}
Clock, Keep Trailing, No trim
array(2) {
  [0]=>
  string(16) " on on on world "
  [1]=>
  string(17) " off
off off
off "
}
Clock, Keep Trailing, trim
array(2) {
  [0]=>
  string(14) "on on on world"
  [1]=>
  string(15) "off
off off
off"
}
Clock, Drop trailing
array(2) {
  [0]=>
  string(16) " on on on world "
  [1]=>
  string(17) " off
off off
off "
}
Clock, Drop trailing, no trim
array(2) {
  [0]=>
  string(16) " on on on world "
  [1]=>
  string(17) " off
off off
off "
}
Clock, Drop trailing, trim
array(2) {
  [0]=>
  string(14) "on on on world"
  [1]=>
  string(15) "off
off off
off"
}
</pre>
</body>
</html>