last update: Mon, 05 Sep 2011 17:16:55 +0000
  1. <?
  2. function escape_string($s)
  3. {
  4. return str_replace('"', '\"', $s);
  5. }
  6. function unescape_string($s)
  7. {
  8. $r = $s;
  9. $r = str_replace('<', '<', $r);
  10. $r = str_replace('>', '>', $r);
  11. $r = str_replace('&', '&', $r);
  12. $r = str_replace('<', '<', $r);
  13. $r = str_replace('>', '>', $r);
  14. $r = str_replace('\\"', '"', $r);
  15. $r = str_replace("\\'", "'", $r);
  16. $r = str_replace('\\\\', '\\', $r);
  17. //$r = str_replace('\\"', '\\"', $r);
  18. return $r;
  19. }
  20. function fill_line_array($s)
  21. {
  22. return explode("\n",$s);
  23. }
  24. class linearraycomparer
  25. {
  26. var $L;
  27. var $A;
  28. var $B;
  29. var $diff;
  30. function linearraycomparer($A, $B)
  31. {
  32. $this->A = $A;
  33. $this->B = $B;
  34. }
  35. function lcs_length()
  36. {
  37. $L = $this->lcs_tree();
  38. if (isset($L[0][0]))
  39. return $L[0][0];
  40. else
  41. return 0;
  42. }
  43. function string_match_percent()
  44. {
  45. $A = $this->A;
  46. $B = $this->B;
  47. $n = max(sizeof($A),sizeof($B));
  48. if ($n > 0)
  49. return $this->lcs_length($A,$B) / $n;
  50. else
  51. return 1;
  52. }
  53. function remove_space($text)
  54. {
  55. $r = str_replace(" ","",$text);
  56. $r = str_replace(" ","",$r);
  57. return $r;
  58. }
  59. function lcs_tree()
  60. {
  61. if ($this->L)
  62. {
  63. return $this->L;
  64. }
  65. else
  66. {
  67. $A = $this->A;
  68. $B = $this->B;
  69. $L = array();
  70. for ($i = sizeof($A)-1; $i >= 0; $i--)
  71. {
  72. for ($j = sizeof($B)-1; $j >= 0; $j--)
  73. {
  74. if (!$A[$i] || !$B[$j])
  75. {
  76. $L[$i][$j] = 0;
  77. }
  78. elseif ($this->remove_space($A[$i]) == $this->remove_space($B[$j]))
  79. {
  80. if (!isset($L[$i+1][$j+1])) $L[$i+1][$j+1] = 0;
  81. $L[$i][$j] = 1 + $L[$i+1][$j+1];
  82. }
  83. else
  84. {
  85. if (!isset($L[$i+1][$j])) $L[$i+1][$j] = 0;
  86. if (!isset($L[$i][$j+1])) $L[$i][$j+1] = 0;
  87. $L[$i][$j] = max($L[$i+1][$j], $L[$i][$j+1]);
  88. }
  89. //echo "$i, $j = ".$L[$i][$j]."<br />";
  90. }
  91. }
  92. $this->L = $L;
  93. return $L;
  94. }
  95. }
  96. function get_string_difference()
  97. {
  98. if ($this->diff)
  99. {
  100. return $this->diff;
  101. }
  102. else
  103. {
  104. $A = $this->A;
  105. $B = $this->B;
  106. $L = $this->lcs_tree();
  107. $i = 0;
  108. $j = 0;
  109. $result = array();
  110. $result[0][0] = 0;
  111. $result[1][0] = 0;
  112. while ($i < sizeof($A) && $j < sizeof($B))
  113. {
  114. if ($this->remove_space($A[$i])==$this->remove_space($B[$j]))
  115. {
  116. $result[0][$i] = 1;
  117. $result[1][$j] = 1;
  118. $i++; $j++;
  119. }
  120. else
  121. {
  122. $result[0][$i] = 0;
  123. $result[1][$j] = 0;
  124. if (!isset($L[$i+1][$j])) $L[$i+1][$j] = 0;
  125. if (!isset($L[$i][$j+1])) $L[$i][$j+1] = 0;
  126. if ($L[$i+1][$j] >= $L[$i][$j+1]) $i++;
  127. else $j++;
  128. }
  129. }
  130. $this->diff = $result;
  131. return $result;
  132. }
  133. }
  134. function make_same_lines_same_height($whichtext)
  135. {
  136. $A = $this->A;
  137. $B = $this->B;
  138. if ($whichtext)
  139. {
  140. $A = $this->B;
  141. $B = $this->A;
  142. }
  143. $diff = $this->get_string_difference();
  144. $diff0 = $diff[$whichtext];
  145. $diff1 = $diff[1-$whichtext];
  146. $result = array();
  147. $j=0;
  148. $i=0;
  149. while ($i < sizeof($A)) //&& ($j < sizeof($B)))
  150. {
  151. if (!isset($diff0[$i])) $diff0[$i] = 0;
  152. if (!isset($diff1[$j])) $diff1[$j] = 1;
  153. if ($diff1[$j] == 0)
  154. {
  155. if ($diff0[$i] == 0)
  156. {
  157. $result [] = $A[$i];
  158. //$j++;
  159. $i++;
  160. }
  161. else
  162. {
  163. $result [] = "////////////////";
  164. $j++;
  165. }
  166. }
  167. else
  168. {
  169. if ($diff0[$i] == 0)
  170. {
  171. //$result [] = "C#".$debug."";
  172. $result [] = $A[$i];
  173. //$j++;
  174. $i++;
  175. }
  176. else
  177. {
  178. $result [] = $A[$i];
  179. $j++;
  180. $i++;
  181. }
  182. }
  183. }
  184. return $result;
  185. }
  186. }
  187. class linecomparer
  188. {
  189. var $L;
  190. var $A;
  191. var $B;
  192. var $diff;
  193. function linecomparer($A, $B)
  194. {
  195. $this->A = $A;
  196. $this->B = $B;
  197. }
  198. function lcs_length()
  199. {
  200. $L = $this->lcs_tree();
  201. if (isset($L[0][0]))
  202. return $L[0][0];
  203. else
  204. return 0;
  205. }
  206. function string_match_percent()
  207. {
  208. $A = $this->A;
  209. $B = $this->B;
  210. $n = max(strlen($A),strlen($B));
  211. if ($n > 0)
  212. return $this->lcs_length($A,$B) / $n;
  213. else
  214. return 1;
  215. }
  216. function lcs_tree()
  217. {
  218. if ($this->L)
  219. {
  220. return $this->L;
  221. }
  222. else
  223. {
  224. $A = $this->A;
  225. $B = $this->B;
  226. $L = array();
  227. for ($i = strlen($A)-1; $i >= 0; $i--)
  228. {
  229. for ($j = strlen($B)-1; $j >= 0; $j--)
  230. {
  231. if (!$A[$i] || !$B[$j])
  232. {
  233. $L[$i][$j] = 0;
  234. }
  235. elseif ($A[$i] == $B[$j])
  236. {
  237. if (!isset($L[$i+1][$j+1])) $L[$i+1][$j+1] = 0;
  238. $L[$i][$j] = 1 + $L[$i+1][$j+1];
  239. }
  240. else
  241. {
  242. if (!isset($L[$i+1][$j])) $L[$i+1][$j] = 0;
  243. if (!isset($L[$i][$j+1])) $L[$i][$j+1] = 0;
  244. $L[$i][$j] = max($L[$i+1][$j], $L[$i][$j+1]);
  245. }
  246. //echo "$i, $j = ".$L[$i][$j]."<br />";
  247. }
  248. }
  249. $this->L = $L;
  250. return $L;
  251. }
  252. }
  253. function get_lcs()
  254. {
  255. $A = $this->A;
  256. $B = $this->B;
  257. $L = $this->lcs_tree();
  258. $i = 0;
  259. $j = 0;
  260. $result = "";
  261. while ($i < strlen($A) && $j < strlen($B))
  262. {
  263. if ($A[$i]==$B[$j])
  264. {
  265. $result.=$A[$i]; //add A[i] to end of S;
  266. $i++; $j++;
  267. }
  268. else
  269. {
  270. if ($L[$i+1][$j] >= $L[$i][$j+1]) $i++;
  271. else $j++;
  272. }
  273. }
  274. return $result;
  275. }
  276. function get_string_difference()
  277. {
  278. if ($this->diff)
  279. {
  280. return $this->diff;
  281. }
  282. else
  283. {
  284. $A = $this->A;
  285. $B = $this->B;
  286. $L = $this->lcs_tree();
  287. $i = 0;
  288. $j = 0;
  289. $result = array();
  290. $result[0][0] = 0;
  291. $result[1][0] = 0;
  292. while ($i < strlen($A) && $j < strlen($B))
  293. {
  294. if ($A[$i]==$B[$j])
  295. {
  296. $result[0][$i] = 1;
  297. $result[1][$j] = 1;
  298. $i++; $j++;
  299. }
  300. else
  301. {
  302. $result[0][$i] = 0;
  303. $result[1][$j] = 0;
  304. if (!isset($L[$i+1][$j])) $L[$i+1][$j] = 0;
  305. if (!isset($L[$i][$j+1])) $L[$i][$j+1] = 0;
  306. if ($L[$i+1][$j] >= $L[$i][$j+1]) $i++;
  307. else $j++;
  308. }
  309. }
  310. $this->diff = $result;
  311. return $result;
  312. }
  313. }
  314. function markup_string_difference($whichstring,$start="<font color=\"red\">",$end="</font>")
  315. {
  316. $A = $this->A;
  317. if ($whichstring)
  318. {
  319. $A = $this->B;
  320. }
  321. $diff = $this->get_string_difference();
  322. $diff = $diff[$whichstring];
  323. $result = "";
  324. $d = 1;
  325. for ($i=0; $i < strlen($A); ++$i)
  326. {
  327. if (!isset($diff[$i])) $diff[$i] = 0;
  328. if ($diff[$i] != $d)
  329. {
  330. if ($d == 1)
  331. {
  332. $result.=$start;
  333. }
  334. else
  335. {
  336. $result.=$end;
  337. }
  338. $d = 1 - $d;
  339. }
  340. $result.=$A[$i];
  341. }
  342. if ($d == 0)
  343. {
  344. $result.=$end;
  345. }
  346. return $result;
  347. }
  348. } //end of class linecomparer
  349. ?>

goto line:
Compare with:
text copy window edit this code post new code