-
Notifications
You must be signed in to change notification settings - Fork 1
/
php-typography.php
2227 lines (1913 loc) · 92.1 KB
/
php-typography.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/*
Project: PHP Typography
Project URI: http://kingdesk.com/projects/php-tyography/
Version: 1.23
Copyright 2009, KINGdesk, LLC. Licensed under the GNU General Public License 2.0. If you use, modify and/or redistribute this software, you must leave the KINGdesk, LLC copyright information, the request for a link to http://kingdesk.com, and the web design services contact information unchanged. If you redistribute this software, or any derivative, it must be released under the GNU General Public License 2.0. This program is distributed without warranty (implied or otherwise) of suitability for any particular purpose. See the GNU General Public License for full license terms <http://creativecommons.org/licenses/GPL/2.0/>.
WE DON'T WANT YOUR MONEY: NO TIPS NECESSARY! If you enjoy this plugin, a link to http://kingdesk.com from your website would be appreciated.
For web design services, please contact jeff@kingdesk.com.
*/
# if used with multibyte language, UTF-8 encoding is required!
class phpTypography {
var $mb = FALSE; //cannot be changed after load
var $chr = array();
var $settings = array(); // operational attributes
var $parsedHTML = array(); // to hold current instance of class parseHTML
var $parsedText = array(); // to hold current instance of class parseText
#=======================================================================
#=======================================================================
#== METHODS - SET ATTRIBUTES
#=======================================================================
#=======================================================================
// __ naming defines constructor that is automatically called on each newly-createy object
function __construct($setDefaults = TRUE) {
$this->chr["noBreakSpace"] = $this->uchr(160);
$this->chr["noBreakNarrowSpace"] = $this->uchr(160); //should be 8239, but not supported consistently, used in unit spacing
$this->chr["copyright"] = $this->uchr(169);
$this->chr["guillemetOpen"] = $this->uchr(171);
$this->chr["softHyphen"] = $this->uchr(173);
$this->chr["registeredMark"] = $this->uchr(174);
$this->chr["guillemetClose"] = $this->uchr(187);
$this->chr["multiplication"] = $this->uchr(215);
$this->chr["division"] = $this->uchr(247);
$this->chr["figureSpace"] = $this->uchr(8199);
$this->chr["thinSpace"] = $this->uchr(8201);
$this->chr["zeroWidthSpace"] = $this->uchr(8203);
$this->chr["hyphen"] = "-"; // should be $this->uchr(8208), but IE6 chokes;
$this->chr["noBreakHyphen"] = $this->uchr(8209);
$this->chr["enDash"] = $this->uchr(8211);
$this->chr["emDash"] = $this->uchr(8212);
$this->chr["singleQuoteOpen"] = $this->uchr(8216); // reset in set_smart_quotes_language()
$this->chr["singleQuoteClose"] = $this->uchr(8217); // reset in set_smart_quotes_language()
$this->chr["apostrophe"] = $this->uchr(8217); // defined seperate from singleQuoteClose so quotes can be redefined in set_smart_quotes_language() without disrupting apostrophies
$this->chr["singleLow9Quote"] = $this->uchr(8218);
$this->chr["doubleQuoteOpen"] = $this->uchr(8220); // reset in set_smart_quotes_language()
$this->chr["doubleQuoteClose"] = $this->uchr(8221); // reset in set_smart_quotes_language()
$this->chr["doubleLow9Quote"] = $this->uchr(8222);
$this->chr["ellipses"] = $this->uchr(8230);
$this->chr["singlePrime"] = $this->uchr(8242);
$this->chr["doublePrime"] = $this->uchr(8243);
$this->chr["singleAngleQuoteOpen"] = $this->uchr(8249);
$this->chr["singleAngleQuoteClose"] = $this->uchr(8250);
$this->chr["fractionSlash"] = $this->uchr(8260);
$this->chr["soundCopyMark"] = $this->uchr(8471);
$this->chr["serviceMark"] = $this->uchr(8480);
$this->chr["tradeMark"] = $this->uchr(8482);
$this->chr["minus"] = $this->uchr(8722);
$this->chr["leftCornerBracket"] = $this->uchr(12300);
$this->chr["rightCornerBracket"] = $this->uchr(12301);
$this->chr["leftWhiteCornerBracket"] = $this->uchr(12302);
$this->chr["rightWhiteCornerBracket"] = $this->uchr(12303);
if($setDefaults) {
$this->set_defaults();
}
return TRUE;
}
function set_defaults() {
// general attributes
$this->set_tags_to_ignore();
$this->set_classes_to_ignore();
$this->set_ids_to_ignore();
//smart characters
$this->set_smart_quotes();
//DEPRECIATED $this->set_smart_quotes_language();
$this->set_smart_quotes_primary(); /* added in version 1.15 */
$this->set_smart_quotes_secondary(); /* added in version 1.15 */
$this->set_smart_dashes();
$this->set_smart_ellipses();
$this->set_smart_diacritics();
$this->set_diacritic_language();
$this->set_diacritic_custom_replacements();
$this->set_smart_marks();
$this->set_smart_ordinal_suffix();
$this->set_smart_math();
$this->set_smart_fractions();
$this->set_smart_exponents();
// DEPRECIATED: $this->set_smart_multiplication();
//smart spacing
$this->set_single_character_word_spacing();
$this->set_fraction_spacing();
$this->set_unit_spacing();
$this->set_punctuation_spacing();
$this->set_units();
$this->set_dash_spacing();
$this->set_dewidow();
$this->set_max_dewidow_length();
$this->set_max_dewidow_pull();
$this->set_wrap_hard_hyphens();
$this->set_url_wrap();
$this->set_email_wrap();
$this->set_min_after_url_wrap();
$this->set_space_collapse();
//character styling
$this->set_style_ampersands();
$this->set_style_caps();
$this->set_style_initial_quotes();
$this->set_style_numbers();
$this->set_initial_quote_tags();
//hyphenation
$this->set_hyphenation();
$this->set_hyphenation_language();
$this->set_min_length_hyphenation();
$this->set_min_before_hyphenation();
$this->set_min_after_hyphenation();
$this->set_hyphenate_headings();
$this->set_hyphenate_all_caps();
$this->set_hyphenate_title_case(); // added in version 1.5
$this->set_hyphenation_exceptions();
return TRUE;
}
// sets tags where typography of children will be untouched
function set_tags_to_ignore($tags = array("code", "head", "kbd", "object", "option", "pre", "samp", "script", "select", "style", "textarea", "title", "var", "math")) {
if(!is_array($tags))
$tags = preg_split("/[\s,]+/", $tags, -1, PREG_SPLIT_NO_EMPTY);
foreach($tags as &$tag){
$tag = strtolower($tag);
}
// self closing tags shouldn't be in $tags
$selfClosingTags = array('area', 'base', 'basefont', 'br', 'frame', 'hr', 'img', 'input', 'link', 'meta');
$tagsCount = count($tags);
// don't use foreach, we need to modify the array we are indexing through
$key = 0; //we need to look through every initial key ($i), but the total key count will reduce over time ($key)
for($i=0; $i<$tagsCount; $i++) {
if(FALSE !== array_search($tags[$key], $selfClosingTags)) {
$tags =array_merge(array_slice($tags, 0, $key), array_slice($tags, $key+1)); // array_merge renumbers numeric keys!
$key--; //adjust for shorter array
}
$key++;
}
// include all inappropriate tags in $tags
$inappropriateTags = array('iframe', 'textarea', 'button', 'select', 'optgroup', 'option' ,'map', 'style', 'head', 'title', 'script', 'applet', 'object', 'param');
foreach($inappropriateTags as $inappropriateTag) {
if(FALSE === array_search($inappropriateTag, $tags)) {
array_push($tags, $inappropriateTag);
}
}
$this->settings["ignoreTags"] = $tags;
return TRUE;
}
// sets classes where typography of children will be untouched
function set_classes_to_ignore($classes = array("vcard", "noTypo")) {
if(!is_array($classes))
$classes = preg_split("/[\s,]+/", $classes, -1, PREG_SPLIT_NO_EMPTY);
$this->settings["ignoreClasses"] = $classes;
return TRUE;
}
// sets IDs where typography of children will be untouched
function set_ids_to_ignore($ids = array()) {
if(!is_array($ids))
$ids = preg_split("/[\s,]+/", $ids, -1, PREG_SPLIT_NO_EMPTY);
$this->settings["ignoreIDs"] = $ids;
return TRUE;
}
// curl quotemarks
function set_smart_quotes($on = TRUE) {
$this->settings["smartQuotes"] = $on;
return TRUE;
}
// DEPRECIATED
// language preferences for curling quotemarks
// allowed values for $lang
// "en" = English style quotes, replaces "foo" with “foo”
// "de" = German style quotes, replaces "foo" with „foo”
// "fr" = French guillemets, replaces "foo" with «foo»
// "fr-reverse" = Reverse French guillemets, replaces "foo" with »foo«
function set_smart_quotes_language($lang = "en") {
if($lang == "de") {
$this->chr["doubleQuoteOpen"] = $this->chr["doubleLow9Quote"];
$this->chr["doubleQuoteClose"] = $this->uchr(8220);
$this->chr["singleQuoteOpen"] = $this->chr["singleLow9Quote"];
$this->chr["singleQuoteClose"] = $this->uchr(8216);
} elseif($lang == "fr") {
$this->chr["doubleQuoteOpen"] = $this->chr["guillemetOpen"];
$this->chr["doubleQuoteClose"] = $this->chr["guillemetClose"];
$this->chr["singleQuoteOpen"] = $this->chr["singleAngleQuoteOpen"];
$this->chr["singleQuoteClose"] = $this->chr["singleAngleQuoteClose"];
} elseif($lang == "fr-reverse") {
$this->chr["doubleQuoteOpen"] = $this->chr["guillemetClose"];
$this->chr["doubleQuoteClose"] = $this->chr["guillemetOpen"];
$this->chr["singleQuoteOpen"] = $this->chr["singleAngleQuoteClose"];
$this->chr["singleQuoteClose"] = $this->chr["singleAngleQuoteOpen"];
} else {
$this->chr["doubleQuoteOpen"] = $this->uchr(8220);
$this->chr["doubleQuoteClose"] = $this->uchr(8221);
$this->chr["singleQuoteOpen"] = $this->uchr(8216);
$this->chr["singleQuoteClose"] = $this->uchr(8217);
}
return TRUE;
}
// Primary quotemarks style
// allowed values for $style
// "doubleCurled" => "“foo”",
// "doubleCurledReversed" => "”foo”",
// "doubleLow9" => "„foo”",
// "doubleLow9Reversed" => "„foo“",
// "singleCurled" => "‘foo’",
// "singleCurledReversed" => "’foo’",
// "singleLow9" => "‚foo’",
// "singleLow9Reversed" => "‚foo‘",
// "doubleGuillemetsFrench" => "« foo »",
// "doubleGuillemets" => "«foo»",
// "doubleGuillemetsReversed" => "»foo«",
// "singleGuillemets" => "‹foo›",
// "singleGuillemetsReversed" => "›foo‹",
// "cornerBrackets" => "「foo」",
// "whiteCornerBracket" => "『foo』",
function set_smart_quotes_primary($style = "doubleCurled") {
if($style == "doubleCurled") {
$this->chr["doubleQuoteOpen"] = $this->uchr(8220);
$this->chr["doubleQuoteClose"] = $this->uchr(8221);
} elseif($style == "doubleCurledReversed") {
$this->chr["doubleQuoteOpen"] = $this->uchr(8221);
$this->chr["doubleQuoteClose"] = $this->uchr(8221);
} elseif($style == "doubleLow9") {
$this->chr["doubleQuoteOpen"] = $this->chr["doubleLow9Quote"];
$this->chr["doubleQuoteClose"] = $this->uchr(8221);
} elseif($style == "doubleLow9Reversed") {
$this->chr["doubleQuoteOpen"] = $this->chr["doubleLow9Quote"];
$this->chr["doubleQuoteClose"] = $this->uchr(8220);
} elseif($style == "singleCurled") {
$this->chr["doubleQuoteOpen"] = $this->uchr(8216);
$this->chr["doubleQuoteClose"] = $this->uchr(8217);
} elseif($style == "singleCurledReversed") {
$this->chr["doubleQuoteOpen"] = $this->uchr(8217);
$this->chr["doubleQuoteClose"] = $this->uchr(8217);
} elseif($style == "singleLow9") {
$this->chr["doubleQuoteOpen"] = $this->chr["singleLow9Quote"];
$this->chr["doubleQuoteClose"] = $this->uchr(8217);
} elseif($style == "singleLow9Reversed") {
$this->chr["doubleQuoteOpen"] = $this->chr["singleLow9Quote"];
$this->chr["doubleQuoteClose"] = $this->uchr(8216);
} elseif($style == "doubleGuillemetsFrench") {
$this->chr["doubleQuoteOpen"] = $this->chr["guillemetOpen"].$this->chr["noBreakSpace"];
$this->chr["doubleQuoteClose"] = $this->chr["noBreakSpace"].$this->chr["guillemetClose"];
} elseif($style == "doubleGuillemets") {
$this->chr["doubleQuoteOpen"] = $this->chr["guillemetOpen"];
$this->chr["doubleQuoteClose"] = $this->chr["guillemetClose"];
} elseif($style == "doubleGuillemetsReversed") {
$this->chr["doubleQuoteOpen"] = $this->chr["guillemetClose"];
$this->chr["doubleQuoteClose"] = $this->chr["guillemetOpen"];
} elseif($style == "singleGuillemets") {
$this->chr["doubleQuoteOpen"] = $this->chr["singleAngleQuoteOpen"];
$this->chr["doubleQuoteClose"] = $this->chr["singleAngleQuoteClose"];
} elseif($style == "singleGuillemetsReversed") {
$this->chr["doubleQuoteOpen"] = $this->chr["singleAngleQuoteClose"];
$this->chr["doubleQuoteClose"] = $this->chr["singleAngleQuoteOpen"];
} elseif($style == "cornerBrackets") {
$this->chr["doubleQuoteOpen"] = $this->chr["leftCornerBracket"];
$this->chr["doubleQuoteClose"] = $this->chr["rightCornerBracket"];
} elseif($style == "whiteCornerBracket") {
$this->chr["doubleQuoteOpen"] = $this->chr["leftWhiteCornerBracket"];
$this->chr["doubleQuoteClose"] = $this->chr["rightWhiteCornerBracket"];
} else {
$this->chr["doubleQuoteOpen"] = $this->uchr(8220);
$this->chr["doubleQuoteClose"] = $this->uchr(8221);
}
return TRUE;
}
// Secondary quotemarks style
// allowed values for $style
// "doubleCurled" => "“foo”",
// "doubleCurledReversed" => "”foo”",
// "doubleLow9" => "„foo”",
// "doubleLow9Reversed" => "„foo“",
// "singleCurled" => "‘foo’",
// "singleCurledReversed" => "’foo’",
// "singleLow9" => "‚foo’",
// "singleLow9Reversed" => "‚foo‘",
// "doubleGuillemetsFrench" => "« foo »",
// "doubleGuillemets" => "«foo»",
// "doubleGuillemetsReversed" => "»foo«",
// "singleGuillemets" => "‹foo›",
// "singleGuillemetsReversed" => "›foo‹",
// "cornerBrackets" => "「foo」",
// "whiteCornerBracket" => "『foo』",
function set_smart_quotes_secondary($style = "singleCurled") {
if($style == "doubleCurled") {
$this->chr["singleQuoteOpen"] = $this->uchr(8220);
$this->chr["singleQuoteClose"] = $this->uchr(8221);
} elseif($style == "doubleCurledReversed") {
$this->chr["singleQuoteOpen"] = $this->uchr(8221);
$this->chr["singleQuoteClose"] = $this->uchr(8221);
} elseif($style == "doubleLow9") {
$this->chr["singleQuoteOpen"] = $this->chr["doubleLow9Quote"];
$this->chr["singleQuoteClose"] = $this->uchr(8221);
} elseif($style == "doubleLow9Reversed") {
$this->chr["singleQuoteOpen"] = $this->chr["doubleLow9Quote"];
$this->chr["singleQuoteClose"] = $this->uchr(8220);
} elseif($style == "singleCurled") {
$this->chr["singleQuoteOpen"] = $this->uchr(8216);
$this->chr["singleQuoteClose"] = $this->uchr(8217);
} elseif($style == "singleCurledReversed") {
$this->chr["singleQuoteOpen"] = $this->uchr(8217);
$this->chr["singleQuoteClose"] = $this->uchr(8217);
} elseif($style == "singleLow9") {
$this->chr["singleQuoteOpen"] = $this->chr["singleLow9Quote"];
$this->chr["singleQuoteClose"] = $this->uchr(8217);
} elseif($style == "singleLow9Reversed") {
$this->chr["singleQuoteOpen"] = $this->chr["singleLow9Quote"];
$this->chr["singleQuoteClose"] = $this->uchr(8216);
} elseif($style == "doubleGuillemetsFrench") {
$this->chr["singleQuoteOpen"] = $this->chr["guillemetOpen"].$this->chr["noBreakSpace"];
$this->chr["singleQuoteClose"] = $this->chr["noBreakSpace"].$this->chr["guillemetClose"];
} elseif($style == "doubleGuillemets") {
$this->chr["singleQuoteOpen"] = $this->chr["guillemetOpen"];
$this->chr["singleQuoteClose"] = $this->chr["guillemetClose"];
} elseif($style == "doubleGuillemetsReversed") {
$this->chr["singleQuoteOpen"] = $this->chr["guillemetClose"];
$this->chr["singleQuoteClose"] = $this->chr["guillemetOpen"];
} elseif($style == "singleGuillemets") {
$this->chr["singleQuoteOpen"] = $this->chr["singleAngleQuoteOpen"];
$this->chr["singleQuoteClose"] = $this->chr["singleAngleQuoteClose"];
} elseif($style == "singleGuillemetsReversed") {
$this->chr["singleQuoteOpen"] = $this->chr["singleAngleQuoteClose"];
$this->chr["singleQuoteClose"] = $this->chr["singleAngleQuoteOpen"];
} elseif($style == "cornerBrackets") {
$this->chr["singleQuoteOpen"] = $this->chr["leftCornerBracket"];
$this->chr["singleQuoteClose"] = $this->chr["rightCornerBracket"];
} elseif($style == "whiteCornerBracket") {
$this->chr["singleQuoteOpen"] = $this->chr["leftWhiteCornerBracket"];
$this->chr["singleQuoteClose"] = $this->chr["rightWhiteCornerBracket"];
} else {
$this->chr["singleQuoteOpen"] = $this->uchr(8216);
$this->chr["singleQuoteClose"] = $this->uchr(8217);
}
return TRUE;
}
// replaces "a--a" with En Dash " -- " and "---" with Em Dash
function set_smart_dashes($on = TRUE) {
$this->settings["smartDashes"] = $on;
return TRUE;
}
// replaces "..." with "…"
function set_smart_ellipses($on = TRUE) {
$this->settings["smartEllipses"] = $on;
return TRUE;
}
// replaces "creme brulee" with "crème brûlée"
function set_smart_diacritics($on = TRUE) {
$this->settings["smartDiacritics"] = $on;
return TRUE;
}
// defines hyphenation language for text
function set_diacritic_language($lang = "en-US") {
if (isset($this->settings["diacriticLanguage"]) && $this->settings["diacriticLanguage"] == $lang)
return TRUE;
$this->settings["diacriticLanguage"] = $lang;
if(file_exists(dirname(__FILE__).'/diacritics/'.$this->settings["diacriticLanguage"].'.php')) {
include('diacritics/'.$this->settings["diacriticLanguage"].'.php');
} else {
include('diacritics/en-US.php');
}
$this->settings["diacriticWords"] = $diacriticWords;
return TRUE;
}
// $customReplacements must be
// an array formatted array(needle=>replacement, needle=>replacement...), or
// a string formatted `"needle"=>"replacement","needle"=>"replacement",...`
function set_diacritic_custom_replacements($customReplacements = array()) {
$replacements = array();
if(!is_array($customReplacements))
$customReplacements = preg_split("/,/", $customReplacements, -1, PREG_SPLIT_NO_EMPTY);
foreach($customReplacements as $customReplacement) {
//account for single and double quotes
preg_match("/(?:\")([^\"]+)(?:\"\s*=>)/", $customReplacement, $doubleQuoteKeyMatch);
preg_match("/(?:')([^']+)(?:'\s*=>)/", $customReplacement, $singleQuoteKeyMatch);
preg_match("/(?:=>\s*\")([^\"]+)(?:\")/", $customReplacement, $doubleQuoteValueMatch);
preg_match("/(?:=>\s*')([^']+)(?:')/", $customReplacement, $singleQuoteValueMatch);
if( isset($doubleQuoteKeyMatch[1]) && ( $doubleQuoteKeyMatch[1] != "" ) ) {
$key = $doubleQuoteKeyMatch[1];
} elseif( isset($singleQuoteKeyMatch[1]) && ( $singleQuoteKeyMatch[1] != "" ) ) {
$key = $singleQuoteKeyMatch[1];
}
if( isset($doubleQuoteValueMatch[1]) && ( $doubleQuoteValueMatch[1] != "" ) ) {
$value = $doubleQuoteValueMatch[1];
} elseif( isset($singleQuoteValueMatch[1]) && ( $singleQuoteValueMatch[1] != "" ) ) {
$value = $singleQuoteValueMatch[1];
}
if( isset($key) && isset($value) ) {
$replacements[strip_tags(trim($key))] = strip_tags(trim($value));
}
}
$this->settings["diacriticCustomReplacements"] = $replacements;
return TRUE;
}
// replaces (r) (c) (tm) (sm) (p) (R) (C) (TM) (SM) (P) with ® © ™ ℠ ℗
function set_smart_marks($on = TRUE) {
$this->settings["smartMarks"] = $on;
return TRUE;
}
// replaces 1/4 with <sup>1</sup>⁄<sub>4</sub>
function set_smart_math($on = TRUE) {
$this->settings["smartMath"] = $on;
return TRUE;
}
// replaces 1/4 with <sup>1</sup>⁄<sub>4</sub>
function set_smart_exponents($on = TRUE) {
$this->settings["smartExponents"] = $on;
return TRUE;
}
// replaces 1/4 with <sup>1</sup>⁄<sub>4</sub>
function set_smart_fractions($on = TRUE) {
$this->settings["smartFractions"] = $on;
return TRUE;
}
// DEPRECIATED
function set_smart_multiplication($on = TRUE) {
$this->settings["smartMath"] = $on;
return TRUE;
}
// wrap numbers in <span class="numbers">
function set_smart_ordinal_suffix($on = TRUE) {
$this->settings["smartOrdinalSuffix"] = $on;
return TRUE;
}
// single character words are forced to next line with insertion of
function set_single_character_word_spacing($on = TRUE) {
$this->settings["singleCharacterWordSpacing"] = $on;
return TRUE;
}
// units and values are kept together with insertion of
function set_fraction_spacing($on = TRUE) {
$this->settings["fractionSpacing"] = $on;
return TRUE;
}
// units and values are kept together with insertion of
function set_unit_spacing($on = TRUE) {
$this->settings["unitSpacing"] = $on;
return TRUE;
}
// words and punctuation are kept together with insertion of
function set_punctuation_spacing($on = TRUE) {
$this->settings["punctuationSpacing"] = $on;
return TRUE;
}
// a list of units to keep with their values
function set_units($units = array()) {
if(!is_array($units))
$units = preg_split("/[\s,]+/", $units, -1, PREG_SPLIT_NO_EMPTY);
$this->settings["units"] = $units;
return TRUE;
}
// Em and En dashes are wrapped in thin spaces
function set_dash_spacing($on = TRUE) {
$this->settings["dashSpacing"] = $on;
return TRUE;
}
// Remove extra space Characters
function set_space_collapse($on = TRUE) {
$this->settings["spaceCollapse"] = $on;
return TRUE;
}
// enables widow handling
function set_dewidow($on = TRUE) {
$this->settings["dewidow"] = $on;
return TRUE;
}
// establishes maximum length of a widows that will be protected
function set_max_dewidow_length($len = 5) {
$len = ($len > 1) ? $len : 5;
$this->settings["dewidowMaxLength"] = $len;
return TRUE;
}
// establishes maximum length of pulled text to keep widows company
function set_max_dewidow_pull($len = 5) {
$len = ($len > 1) ? $len : 5;
$this->settings["dewidowMaxPull"] = $len;
return TRUE;
}
// enables wrapping at hard hyphens internal to a word with the insertion of a zero-width-space
function set_wrap_hard_hyphens($on = TRUE) {
$this->settings["hyphenHardWrap"] = $on;
return TRUE;
}
// enables wrapping of urls
function set_url_wrap($on = TRUE) {
$this->settings["urlWrap"] = $on;
return TRUE;
}
// enables wrapping of email addresses
function set_email_wrap($on = TRUE) {
$this->settings["emailWrap"] = $on;
return TRUE;
}
// establishes minimum character requirement after a url wrapping point
function set_min_after_url_wrap($len = 5) {
$len = ($len > 0) ? $len : 5;
$this->settings["urlMinAfterWrap"] = $len;
return TRUE;
}
// wrap ampersands in <span class="amp">
function set_style_ampersands($on = TRUE) {
$this->settings["styleAmpersands"] = $on;
return TRUE;
}
// wrap caps in <span class="caps">
function set_style_caps($on = TRUE) {
$this->settings["styleCaps"] = $on;
return TRUE;
}
// wrap initial quotes in <span class="quo"> or <span class="dquo">
function set_style_initial_quotes($on = TRUE) {
$this->settings["styleInitialQuotes"] = $on;
return TRUE;
}
// wrap numbers in <span class="numbers">
function set_style_numbers($on = TRUE) {
$this->settings["styleNumbers"] = $on;
return TRUE;
}
// sets tags where initial quotes and guillemets should be styled
function set_initial_quote_tags($tags = array("p", "h1", "h2", "h3", "h4", "h5", "h6", "blockquote", "li", "dd", "dt")) {
if(!is_array($tags))
$tags = preg_split("/[^a-z0-9]+/", $tags, -1, PREG_SPLIT_NO_EMPTY);
foreach($tags as &$tag){
$tag = strtolower($tag);
}
$this->settings["initialQuoteTags"] = $tags;
return TRUE;
}
// enables hyphenation of text
function set_hyphenation($on = TRUE) {
$this->settings["hyphenation"] = $on;
return TRUE;
}
// defines hyphenation language for text
function set_hyphenation_language($lang = "en-US") {
if (isset($this->settings["hyphenLanguage"]) && $this->settings["hyphenLanguage"] == $lang)
return TRUE;
$this->settings["hyphenLanguage"] = $lang;
if(file_exists(dirname(__FILE__).'/lang/'.$this->settings["hyphenLanguage"].'.php')) {
include('lang/'.$this->settings["hyphenLanguage"].'.php');
} else {
include('lang/en-US.php');
}
$this->settings["hyphenationPattern"] = $patgen;
$this->settings["hyphenationPatternMaxSegment"] = $patgenMaxSeg;
$this->settings["hyphenationPatternExceptions"] = $patgenExceptions;
// make sure hyphenationExceptions is not set to force remerging of patgen and custom exceptions
if(isset($this->settings["hyphenationExceptions"])) unset($this->settings["hyphenationExceptions"]);
return TRUE;
}
// establishes minimum length of a word that may be hyphenated
function set_min_length_hyphenation($len = 5) {
$len = ($len > 1) ? $len : 5;
$this->settings["hyphenMinLength"] = $len;
return TRUE;
}
// establishes minimum character requirement before a hyphenation point
function set_min_before_hyphenation($len = 3) {
$len = ($len > 0) ? $len : 3;
$this->settings["hyphenMinBefore"] = $len;
return TRUE;
}
// establishes minimum character requirement after a hyphenation point
function set_min_after_hyphenation($len = 2) {
$len = ($len > 0) ? $len : 2;
$this->settings["hyphenMinAfter"] = $len;
return TRUE;
}
// allows/disallows hyphenation of title/heading text
function set_hyphenate_headings($on = TRUE) {
$this->settings["hyphenateTitle"] = $on;
return TRUE;
}
// allows hyphenation of strings of all capital characters
function set_hyphenate_all_caps($on = TRUE) {
$this->settings["hyphenateAllCaps"] = $on;
return TRUE;
}
// allows hyphenation of strings of all capital characters
// added in version 1.5
function set_hyphenate_title_case($on = TRUE) {
$this->settings["hyphenateTitleCase"] = $on;
return TRUE;
}
// defines custom word hyphenations
// expected input is an array of words with all hyphenation points marked with a hard hyphen
function set_hyphenation_exceptions($exceptions = array()) {
$encodings = array("ASCII","UTF-8", "ISO-8859-1");
$multibyte = FALSE;
$u = "";
if(!is_array($exceptions))
$exceptions = preg_split("/[^a-zA-Z0-9\-]+/", $exceptions, -1, PREG_SPLIT_NO_EMPTY);
$exceptionKeys = array();
foreach($exceptions as $key => &$exception) {
$encoding = mb_detect_encoding($exception."a", $encodings);
if("UTF-8" == $encoding) {
$multibyte = TRUE;
$u = "u";
if(!function_exists('mb_strlen')) return FALSE;
} elseif("ASCII" == $encoding) {
$multibyte = FALSE;
} else {
return FALSE;
}
if($multibyte) {
$exception = mb_strtolower($exception, "UTF-8");
} else { //same as above without multibyte string functions to improve preformance
$exception = strtolower($exception);
}
$exceptionKeys[$key] = preg_replace("#-#$u", "", $exception);
}
$e = array();
foreach($exceptionKeys as $key => $value) {
$e[$value] = $exceptions[$key];
}
$this->settings["hyphenationCustomExceptions"] = $e;
// make sure hyphenationExceptions is not set to force remerging of patgen and custom exceptions
if(isset($this->settings["hyphenationExceptions"])) unset($this->settings["hyphenationExceptions"]);
return TRUE;
}
#=======================================================================
#=======================================================================
#== METHODS - ACTIONS, let's do something!
#=======================================================================
#=======================================================================
# Returns: ARRAY of supported hyphenation languages in the form array( language code => language name)
function get_languages() {
$languages = array();
$langDir = dirname(__FILE__)."/lang/";
$handler = opendir($langDir);
// read all files in directory
while ($file = readdir($handler)) {
// we only want the php files
if (substr($file, -4) == ".php") {
$fileContent = file_get_contents($langDir.$file);
preg_match('/\$patgenLanguage\s*=\s*((".+")|(\'.+\'))\s*;/', $fileContent, $matches);
$languageName = substr($matches[1], 1, -1);
$languageCode = substr($file, 0, -4);
$results[$languageCode] = $languageName;
}
}
closedir($handler);
asort($results);
return $results;
}
# Returns: ARRAY of supported hyphenation languages in the form array( language code => language name)
function get_diacritic_languages() {
$languages = array();
$langDir = dirname(__FILE__)."/diacritics/";
$handler = opendir($langDir);
// read all files in directory
while ($file = readdir($handler)) {
// we only want the php files
if (substr($file, -4) == ".php") {
$fileContent = file_get_contents($langDir.$file);
preg_match('/\$diacriticLanguage\s*=\s*((".+")|(\'.+\'))\s*;/', $fileContent, $matches);
$languageName = substr($matches[1], 1, -1);
$languageCode = substr($file, 0, -4);
$results[$languageCode] = $languageName;
}
}
closedir($handler);
asort($results);
return $results;
}
# Action: modifies $html according to the defined settings
# Returns: processed $html
function process($html, $isTitle = FALSE) {
if( isset($this->settings["ignoreTags"] ) && $isTitle && ( in_array('h1', $this->settings["ignoreTags"]) || in_array('h2', $this->settings["ignoreTags"]) ) )
return $html;
require_once("php-parser/php-parser.php");
// parse the html
$this->parsedHTML = new parseHTML();
$this->parsedHTML->load($html);
$this->parsedHTML->unlock_text();
$tagsToIgnore = $this->parsedHTML->get_tags_by_name($this->settings["ignoreTags"]);
if(isset($this->settings["ignoreClasses"]))
$tagsToIgnore += $this->parsedHTML->get_tags_by_class($this->settings["ignoreClasses"]); //union to avoid dup keys
if(isset($this->settings["ignoreIDs"]))
$tagsToIgnore += $this->parsedHTML->get_tag_by_id($this->settings["ignoreIDs"]); //union to avoid dup keys
$this->parsedHTML->lock_children($tagsToIgnore);
$unlockedTexts = $this->parsedHTML->get_unlocked_text();
foreach($unlockedTexts as &$unlockedText) {
// we won't be doing anything with spaces, so we can jump ship if that is all we have
if (0 == strlen(trim($unlockedText["value"]))) continue;
// decode all characters except < > &
$unlockedText["value"] = html_entity_decode($unlockedText["value"], ENT_QUOTES, "UTF-8"); //converts all HTML entities to their applicable characters
$unlockedText["value"] = htmlspecialchars($unlockedText["value"], ENT_NOQUOTES, "UTF-8"); //returns < > & to encoded HTML characters (< > and & respectively)
// modify anything that requires adjacent text awareness here
$unlockedText = $this->smart_math($unlockedText);
$unlockedText = $this->smart_diacritics($unlockedText);
$unlockedText = $this->smart_quotes($unlockedText);
$unlockedText = $this->smart_dashes($unlockedText);
$unlockedText = $this->smart_ellipses($unlockedText);
$unlockedText = $this->smart_marks($unlockedText);
//keep spacing after smart character replacement
$unlockedText = $this->single_character_word_spacing($unlockedText);
$unlockedText = $this->dash_spacing($unlockedText);
$unlockedText = $this->unit_spacing($unlockedText);
$unlockedText = $this->punctuation_spacing($unlockedText);
//break it down for a bit more granularity
$this->parsedText = new parseText();
$this->parsedText->load($unlockedText);
$parsedMixedWords = $this->parsedText->get_words(-1,0); // prohibit letter only words, allow caps
$caps = (isset($this->settings["hyphenateAllCaps"]) && $this->settings["hyphenateAllCaps"]) ? 0 : -1 ;
$parsedWords = $this->parsedText->get_words(1,$caps); // require letter only words, caps allowance in settingibutes; mutually exclusive with $parsedMixedWords
$parsedOther = $this->parsedText->get_other();
// process individual text parts here
$parsedMixedWords = $this->wrap_hard_hyphens($parsedMixedWords);
$parsedWords = $this->hyphenate($parsedWords, $isTitle);
$parsedOther = $this->wrap_urls($parsedOther);
$parsedOther = $this->wrap_emails($parsedOther);
//apply updates to unlockedText
$this->parsedText->update($parsedMixedWords+$parsedWords+$parsedOther);
$unlockedText = $this->parsedText->unload();
//some final space manipulation
$unlockedText = $this->dewidow($unlockedText);
$unlockedText = $this->space_collapse($unlockedText);
//everything that requires HTML injection occurs here (functions above assume tag-free content)
//pay careful attention to functions below for tolerance of injected tags
$unlockedText = $this->smart_ordinal_suffix($unlockedText); // call before "style_numbers" and "smart_fractions"
$unlockedText = $this->smart_exponents($unlockedText); // call before "style_numbers"
$unlockedText = $this->smart_fractions($unlockedText); // call before "style_numbers" and after "smart_ordinal_suffix"
if(!$this->parsedHTML->in_class('caps', $unlockedText))
$unlockedText = $this->style_caps($unlockedText); // call before "style_numbers"
if(!$this->parsedHTML->in_class('numbers', $unlockedText))
$unlockedText = $this->style_numbers($unlockedText); // call after "smart_ordinal_suffix", "smart_exponents", "smart_fractions", and "style_caps"
if(!$this->parsedHTML->in_class('amp', $unlockedText))
$unlockedText = $this->style_ampersands($unlockedText);
if(!$this->parsedHTML->in_class(array('quo','dquo'), $unlockedText))
$unlockedText = $this->style_initial_quotes($unlockedText, $isTitle);
}
$this->parsedHTML->update($unlockedTexts);
return $this->parsedHTML->unload();
}
# Action: modifies $html according to the defined settings as only appropriate for RSS feeds
# (i.e. excluding processes that may not display well with limited character set inteligence)
# Returns: processed $html
function process_feed($html, $isTitle = FALSE) {
if( isset($this->settings["ignoreTags"]) && $isTitle && ( in_array('h1', $this->settings["ignoreTags"]) || in_array('h2', $this->settings["ignoreTags"]) ) )
return $html;
require_once("php-parser/php-parser.php");
// parse the html
$this->parsedHTML = new parseHTML();
$this->parsedHTML->load($html);
$this->parsedHTML->unlock_text();
$tagsToIgnore = $this->parsedHTML->get_tags_by_name($this->settings["ignoreTags"]);
if(isset($this->settings["ignoreClasses"]))
$tagsToIgnore += $this->parsedHTML->get_tags_by_class($this->settings["ignoreClasses"]); //union to avoid dup keys
if(isset($this->settings["ignoreIDs"]))
$tagsToIgnore += $this->parsedHTML->get_tag_by_id($this->settings["ignoreIDs"]); //union to avoid dup keys
$this->parsedHTML->lock_children($tagsToIgnore);
$unlockedTexts = $this->parsedHTML->get_unlocked_text();
foreach($unlockedTexts as &$unlockedText) {
// we won't be doing anything with spaces, so we can jump ship if that is all we have
if (0 == strlen(trim($unlockedText["value"]))) continue;
// decode all characters except < > &
$unlockedText["value"] = html_entity_decode($unlockedText["value"], ENT_QUOTES, "UTF-8"); //converts all HTML entities to their applicable characters
$unlockedText["value"] = htmlspecialchars($unlockedText["value"], ENT_NOQUOTES, "UTF-8"); //returns < > & to encoded HTML characters (< > and & respectively)
// modify anything that requires adjacent text awareness here
$unlockedText = $this->smart_quotes($unlockedText);
$unlockedText = $this->smart_dashes($unlockedText);
$unlockedText = $this->smart_ellipses($unlockedText);
$unlockedText = $this->smart_marks($unlockedText);
}
// add $initialChrs and $widows back into $unlockedTexts;
$this->parsedHTML->update($unlockedTexts);
return $this->parsedHTML->unload();
}
#=======================================================================
#=======================================================================
#== OTHER METHODS
#=======================================================================
#=======================================================================
//expecting parsedHTML token of type text
function smart_quotes($parsedHTMLtoken) {
if(!isset($this->settings["smartQuotes"]) || !$this->settings["smartQuotes"]) return $parsedHTMLtoken;
$nonEnglishWordCharacters = "
[0-9A-Za-z]|\x{00c0}|\x{00c1}|\x{00c2}|\x{00c3}|\x{00c4}|\x{00c5}|\x{00c6}|\x{00c7}|\x{00c8}|\x{00c9}|
\x{00ca}|\x{00cb}|\x{00cc}|\x{00cd}|\x{00ce}|\x{00cf}|\x{00d0}|\x{00d1}|\x{00d2}|\x{00d3}|\x{00d4}|
\x{00d5}|\x{00d6}|\x{00d8}|\x{00d9}|\x{00da}|\x{00db}|\x{00dc}|\x{00dd}|\x{00de}|\x{00df}|\x{00e0}|
\x{00e1}|\x{00e2}|\x{00e3}|\x{00e4}|\x{00e5}|\x{00e6}|\x{00e7}|\x{00e8}|\x{00e9}|\x{00ea}|\x{00eb}|
\x{00ec}|\x{00ed}|\x{00ee}|\x{00ef}|\x{00f0}|\x{00f1}|\x{00f2}|\x{00f3}|\x{00f4}|\x{00f5}|\x{00f6}|
\x{00f8}|\x{00f9}|\x{00fa}|\x{00fb}|\x{00fc}|\x{00fd}|\x{00fe}|\x{00ff}|\x{0100}|\x{0101}|\x{0102}|
\x{0103}|\x{0104}|\x{0105}|\x{0106}|\x{0107}|\x{0108}|\x{0109}|\x{010a}|\x{010b}|\x{010c}|\x{010d}|
\x{010e}|\x{010f}|\x{0110}|\x{0111}|\x{0112}|\x{0113}|\x{0114}|\x{0115}|\x{0116}|\x{0117}|\x{0118}|
\x{0119}|\x{011a}|\x{011b}|\x{011c}|\x{011d}|\x{011e}|\x{011f}|\x{0120}|\x{0121}|\x{0122}|\x{0123}|
\x{0124}|\x{0125}|\x{0126}|\x{0127}|\x{0128}|\x{0129}|\x{012a}|\x{012b}|\x{012c}|\x{012d}|\x{012e}|
\x{012f}|\x{0130}|\x{0131}|\x{0132}|\x{0133}|\x{0134}|\x{0135}|\x{0136}|\x{0137}|\x{0138}|\x{0139}|
\x{013a}|\x{013b}|\x{013c}|\x{013d}|\x{013e}|\x{013f}|\x{0140}|\x{0141}|\x{0142}|\x{0143}|\x{0144}|
\x{0145}|\x{0146}|\x{0147}|\x{0148}|\x{0149}|\x{014a}|\x{014b}|\x{014c}|\x{014d}|\x{014e}|\x{014f}|
\x{0150}|\x{0151}|\x{0152}|\x{0153}|\x{0154}|\x{0155}|\x{0156}|\x{0157}|\x{0158}|\x{0159}|\x{015a}|
\x{015b}|\x{015c}|\x{015d}|\x{015e}|\x{015f}|\x{0160}|\x{0161}|\x{0162}|\x{0163}|\x{0164}|\x{0165}|
\x{0166}|\x{0167}|\x{0168}|\x{0169}|\x{016a}|\x{016b}|\x{016c}|\x{016d}|\x{016e}|\x{016f}|\x{0170}|
\x{0171}|\x{0172}|\x{0173}|\x{0174}|\x{0175}|\x{0176}|\x{0177}|\x{0178}|\x{0179}|\x{017a}|\x{017b}|
\x{017c}|\x{017d}|\x{017e}|\x{017f}
";
//need to get context of adjacent characters outside adjacent inline tags or HTML comment
//if we have adjacent characters add them to the text
$nextChr = "";
$prevChr = "";
if(isset($parsedHTMLtoken["prevChr"]) && $parsedHTMLtoken["prevChr"] != "") {
$prevChr = $parsedHTMLtoken["prevChr"];
$parsedHTMLtoken["value"] = $prevChr.$parsedHTMLtoken["value"];
}
if(isset($parsedHTMLtoken["nextChr"]) && $parsedHTMLtoken["nextChr"] != "") {
$nextChr = $parsedHTMLtoken["nextChr"];
$parsedHTMLtoken["value"] = $parsedHTMLtoken["value"].$nextChr;
}
////Logic
// before primes, handle quoted numbers
$parsedHTMLtoken["value"] = preg_replace("/(?<=\W|\A)'(\d+)'(?=\W|\Z)/u", $this->chr["singleQuoteOpen"].'$1'.$this->chr["singleQuoteClose"], $parsedHTMLtoken["value"]);
$parsedHTMLtoken["value"] = preg_replace("/(?<=\W|\A)\"(\d+)\"(?=\W|\Z)/u", $this->chr["doubleQuoteOpen"].'$1'.$this->chr["doubleQuoteClose"], $parsedHTMLtoken["value"]);
// guillemets
$parsedHTMLtoken["value"] = str_replace("<<", $this->chr["guillemetOpen"], $parsedHTMLtoken["value"]);
$parsedHTMLtoken["value"] = str_replace("<<", $this->chr["guillemetOpen"], $parsedHTMLtoken["value"]);
$parsedHTMLtoken["value"] = str_replace(">>", $this->chr["guillemetClose"], $parsedHTMLtoken["value"]);
$parsedHTMLtoken["value"] = str_replace(">>", $this->chr["guillemetClose"], $parsedHTMLtoken["value"]);
// primes
$parsedHTMLtoken["value"] = preg_replace("/(\b\d+)''(?=\W|\Z)/u", '$1'.$this->chr["doublePrime"], $parsedHTMLtoken["value"]);
$parsedHTMLtoken["value"] = preg_replace("/(\b\d+)\"(?=\W|\Z)/u", '$1'.$this->chr["doublePrime"], $parsedHTMLtoken["value"]);
$parsedHTMLtoken["value"] = preg_replace("/(\b\d+)'(?=\W|\Z)/u", '$1'.$this->chr["singlePrime"], $parsedHTMLtoken["value"]);
// backticks
$parsedHTMLtoken["value"] = str_replace("``", $this->chr["doubleQuoteOpen"], $parsedHTMLtoken["value"]);
$parsedHTMLtoken["value"] = str_replace("`", $this->chr["singleQuoteOpen"], $parsedHTMLtoken["value"]);
$parsedHTMLtoken["value"] = str_replace("''", $this->chr["doubleQuoteClose"], $parsedHTMLtoken["value"]);
// comma quotes
$parsedHTMLtoken["value"] =str_replace(",,", $this->chr["doubleLow9Quote"], $parsedHTMLtoken["value"]);
$parsedHTMLtoken["value"] = preg_replace("/(?<=\s|\A),(?=\S)/", $this->chr["singleLow9Quote"], $parsedHTMLtoken["value"]); //like _,¿hola?'_
// apostrophes
$parsedHTMLtoken["value"] = preg_replace("/(?<=[\w|$nonEnglishWordCharacters])'(?=[\w|$nonEnglishWordCharacters])/u", $this->chr["apostrophe"], $parsedHTMLtoken["value"]);
$parsedHTMLtoken["value"] = preg_replace("/'(\d\d\b)/", $this->chr["apostrophe"].'$1', $parsedHTMLtoken["value"]); // decades: '98
$exceptions = array("'tain".$this->chr["apostrophe"]."t", "'twere", "'twas", "'tis", "'til", "'bout", "'nuff", "'round", "'cause", "'splainin");
$replacements = array($this->chr["apostrophe"]."tain".$this->chr["apostrophe"]."t", $this->chr["apostrophe"]."twere", $this->chr["apostrophe"]."twas", $this->chr["apostrophe"]."tis", $this->chr["apostrophe"]."til", $this->chr["apostrophe"]."bout", $this->chr["apostrophe"]."nuff", $this->chr["apostrophe"]."round", $this->chr["apostrophe"]."cause", $this->chr["apostrophe"]."splainin");
$parsedHTMLtoken["value"] = str_replace($exceptions, $replacements, $parsedHTMLtoken["value"]);
//quotes
$quoteRules = array("['", "{'", "('", "']", "'}", "')", "[\"", "{\"", "(\"", "\"]", "\"}", "\")", "\"'", "'\"");
$quoteRulesReplace = array("[".$this->chr["singleQuoteOpen"], "{".$this->chr["singleQuoteOpen"], "(".$this->chr["singleQuoteOpen"], $this->chr["singleQuoteClose"]."]", $this->chr["singleQuoteClose"]."}", $this->chr["singleQuoteClose"].")", "[".$this->chr["doubleQuoteOpen"], "{".$this->chr["doubleQuoteOpen"], "(".$this->chr["doubleQuoteOpen"], $this->chr["doubleQuoteClose"]."]", $this->chr["doubleQuoteClose"]."}", $this->chr["doubleQuoteClose"].")", $this->chr["doubleQuoteOpen"].$this->chr["singleQuoteOpen"], $this->chr["singleQuoteClose"].$this->chr["doubleQuoteClose"]);
$parsedHTMLtoken["value"] =str_replace($quoteRules, $quoteRulesReplace, $parsedHTMLtoken["value"]);
$parsedHTMLtoken["value"] = preg_replace("/'(?=[\w|$nonEnglishWordCharacters])/u", $this->chr["singleQuoteOpen"], $parsedHTMLtoken["value"]);
$parsedHTMLtoken["value"] = preg_replace("/(?<=[\w|$nonEnglishWordCharacters])'/u", $this->chr["singleQuoteClose"], $parsedHTMLtoken["value"]);
$parsedHTMLtoken["value"] = preg_replace("/(?<=\s|\A)'(?=\S)/", $this->chr["singleQuoteOpen"], $parsedHTMLtoken["value"]); //like _'¿hola?'_
$parsedHTMLtoken["value"] = preg_replace("/(?<=\S)'(?=\s|\Z)/", $this->chr["singleQuoteClose"], $parsedHTMLtoken["value"]);
$parsedHTMLtoken["value"] = preg_replace("/\"(?=[\w|$nonEnglishWordCharacters])/u", $this->chr["doubleQuoteOpen"], $parsedHTMLtoken["value"]);
$parsedHTMLtoken["value"] = preg_replace("/(?<=[\w|$nonEnglishWordCharacters])\"/u", $this->chr["doubleQuoteClose"], $parsedHTMLtoken["value"]);
$parsedHTMLtoken["value"] = preg_replace("/(?<=\s|\A)\"(?=\S)/", $this->chr["doubleQuoteOpen"], $parsedHTMLtoken["value"]);
$parsedHTMLtoken["value"] = preg_replace("/(?<=\S)\"(?=\s|\Z)/", $this->chr["doubleQuoteClose"], $parsedHTMLtoken["value"]);
//quote catch-alls - assume left over quotes are closing - as this is often the most complicated position, thus most likely to be missed
$parsedHTMLtoken["value"] = str_replace("'", $this->chr["singleQuoteClose"], $parsedHTMLtoken["value"]);
$parsedHTMLtoken["value"] = str_replace('"', $this->chr["doubleQuoteClose"], $parsedHTMLtoken["value"]);
//if we have adjacent characters remove them from the text
$encodings = array("ASCII","UTF-8");
$e = mb_detect_encoding($parsedHTMLtoken["value"]."a", $encodings);// ."a" is a hack; see http://www.php.net/manual/en/function.mb-detect-encoding.php#81936