PDA

Orijinalini görmek için tıklayınız : string işlemlerini kolayca yapmanızı sağlayan basit bir class


tepisenordek
08-11-2007, 20:33 PM
PHP:

1.
2. <?php
3.
4. if (!defined('DEFAULT_CHARSET')) {
5. define('DEFAULT_CHARSET','UTF-8');
6. }
7.
8. /**
9. * improved string class provides easy methods for string manipulations
10. *
11. * @author Engin Dumlu
12. * @copyright turk-php.com
13. * @version 0.1
14. */
15. class istring{
16.
17. public $v = null;
18.
19.
20. /**
21. * Constructor
22. *
23. * @param string $string
24. */
25. function __construct($string=null){
26. $this->v = $string;
27. }
28.
29. /**
30. * returns data when class converted to string
31. *
32. * @return string
33. */
34. function __toString(){
35. return $this->v;
36. }
37.
38.
39. /**
40. * set new value
41. *
42. * @param string $val
43. * @return istring
44. */
45. function set($val=null){
46. $this->v = $val;
47. return $this;
48. }
49.
50. /**
51. * math addition
52. *
53. * @return istring
54. */
55. function addition($int){
56. $this->v = $this->addition_r($int);
57. return $this;
58. }
59. /**
60. * math addition
61. *
62. * @return int
63. */
64. function addition_r($int){
65. return $this->v + $int;
66. }
67. /**
68. * math multiply
69. *
70. * @return istring
71. */
72. function multiply($num){
73. $this->v = $this->multiply_r($num);
74. return $this;
75. }
76. /**
77. * math multiply
78. *
79. * @return int
80. */
81. function multiply_r($num){
82. return $this->v*$num;
83. }
84. /**
85. * math divide
86. *
87. * @return istring
88. */
89. function divide($num){
90. $this->v = $this->divide_r($num);
91. return $this;
92. }
93. /**
94. * math divide
95. *
96. * @return int
97. */
98. function divide_r($num){
99. return (($num !=0) ? $this->v/$num : 0);
100. }
101.
102.
103. /**
104. * convert data to numeric boolean 1 | 0
105. *
106. * @return istring
107. */
108. function toNumericBoolean(){
109. $this->v = $this->toNumericBoolean_r();
110. return $this;
111. }
112.
113. /**
114. * convert data to numeric boolean 1 | 0
115. *
116. * @return integer
117. */
118. function toNumericBoolean_r(){
119. return (!$this->isEmpty() && $this->preg_match('/^[^false|0]$/')) ? 1 : 0;
120. }
121.
122. /**
123. * convert all applicable characters to html entities
124. *
125. * @param int $quote_style
126. * @param string $charset
127. * @return istring
128. */
129. function htmlentities($quote_style=ENT_QUOTES,$charset=DEFA ULT_CHARSET){
130. $this->v = $this->htmlentities_r($quote_style,$charset);
131. return $this;
132. }
133.
134. /**
135. * convert special characters to html entities and get the affected data
136. *
137. * @param int $quote_style
138. * @param string $charset
139. * @return string
140. */
141. function htmlentities_r($quote_style=ENT_QUOTES,$charset=DE FAULT_CHARSET){
142. return htmlentities($this->v,$quote_style,$charset);
143. }
144.
145.
146. /**
147. * convert special characters to html entities
148. *
149. * @param int $quote_style
150. * @param string $charset
151. * @return istring
152. */
153. function htmlspecialchars($quote_style=ENT_QUOTES,$charset= DEFAULT_CHARSET){
154. $this->v = $this->htmlspecialchars_r($quote_style,$charset);
155. return $this;
156. }
157.
158. /**
159. * convert special characters to html entities and get the affected data
160. *
161. * @param int $quote_style
162. * @param string $charset
163. * @return string
164. */
165. function htmlspecialchars_r($quote_style=ENT_QUOTES,$charse t=DEFAULT_CHARSET){
166. return htmlspecialchars($this->v,$quote_style,$charset);
167. }
168.
169.
170.
171. /**
172. * escape from javascript
173. *
174. * @param boolean $deep
175. * @return istring
176. */
177. function escapeJavascript($deep=false){
178. $this->v = $this->escapeJavascript_r($deep);
179. return $this;
180. }
181.
182. /**
183. * escape from javascript
184. *
185. * @param boolean $deep
186. * @return istring
187. */
188. function escapeJavascript_r($deep=false){
189. $search = array ('@<script[^>]*?>.*?</script>@si','@([\r\n])[\s]+@si');
190. $replace = array ('','');
191. if ($deep) {
192. $search[] = '/[\s]+on[\w]{2,15}=[\"|\'][^>]*?[\"|\']/im';
193. $replace[] = '';
194. }
195. return preg_replace($search, $replace, "".$this->v."");
196. }
197.
198.
199. /**
200. * makes string uppercase
201. *
202. * @return istring
203. */
204. function upper(){
205. $this->v = $this->upper_r();
206. return $this;
207. }
208.
209. /**
210. * makes string uppercase and return affected string
211. *
212. * @return string
213. */
214. function upper_r(){
215. return mb_strtoupper($this->v,DEFAULT_CHARSET);
216. }
217.
218. /**
219. * makes string lowercase
220. *
221. * @return istring
222. */
223. function lower(){
224. $this->v = $this->lower_r();
225. return $this;
226. }
227.
228.
229. /**
230. * makes string lowercase and return affected string
231. *
232. * @return string
233. */
234. function lower_r(){
235. return mb_strtolower($this->v,DEFAULT_CHARSET);
236. }
237.
238.
239. /**
240. * escape against mysql
241. *
242. * @return istring
243. */
244. function mysql_escape(){
245. $this->v = $this->mysql_escape_r();
246. return $this;
247. }
248.
249. /**
250. * escape against mysql and returns affected string [ doesnt modify the orjinal string ]
251. *
252. * @return string
253. */
254. function mysql_escape_r(){
255. return mysql::escape($this->v);
256. }
257.
258. /**
259. * unescape against mysql
260. *
261. * @return istring
262. */
263. function mysql_unescape(){
264. $this->v = $this->mysql_unescape_r();
265. return $this;
266. }
267.
268. /**
269. * unescape against mysql and returns affected string [ doesnt modify the orjinal string ]
270. *
271. * @return string
272. */
273. function mysql_unescape_r(){
274. return stripslashes($this->v);
275. }
276.
277. /**
278. * print data
279. *
280. */
281. function _echo(){
282. echo $this->v;
283. }
284.
285. /**
286. * print data
287. *
288. */
289. function _print(){
290. print $this->v;
291. }
292.
293. /**
294. * dump data
295. *
296. */
297. function _dump(){
298. var_dump($this->v);
299. }
300.
301. /**
302. * lenght of the string
303. *
304. * @return int
305. */
306. function length(){
307. return mb_strlen($this->v,DEFAULT_CHARSET);
308. }
309.
310. /**
311. * get THE data
312. *
313. * @return string
314. */
315. function render(){
316. return $this->v;
317. }
318.
319. /**
320. * get a part of the string
321. *
322. * @param int $start
323. * @param int $length
324. * @param int $encoding
325. * @return istring
326. */
327. function substr($start,$length=null,$encoding=DEFAULT_CHARS ET){
328. $this->v = $this->substr_r($start,$length,$encoding);
329. return $this;
330. }
331.
332. /**
333. * get a part of the string
334. *
335. * @param int $start
336. * @param int $length
337. * @param int $encoding
338. * @return string
339. */
340. function substr_r($start,$length=null,$encoding=DEFAULT_CHA RSET){
341. return mb_substr($this->v,$start,$length,$encoding);
342. }
343.
344.
345. /**
346. * Make a string's first character uppercase
347. *
348. * @return istring
349. */
350. function ucfirst(){
351. $this->v = $this->ucfirst_r();
352. return $this;
353. }
354.
355. /**
356. * Make a string's first character uppercase and get affected string
357. *
358. * @return string
359. */
360. function ucfirst_r(){
361. return
362. mb_strtoupper($this->substr_r(0,1),DEFAULT_CHARSET).mb_strtolower($this->substr_r(1,$this->length()-1,DEFAULT_CHARSET),DEFAULT_CHARSET);
363. }
364.
365.
366.
367. /**
368. * split the string by string
369. *
370. * @param string $seperator
371. * @param int $limit
372. * @return array
373. */
374. function explode($seperator,$limit=null){
375. return (!$limit) ? explode($seperator,$this->v) : explode($seperator,$this->v,$limit);
376. }
377.
378. /**
379. * convert the string to an array [php5]
380. *
381. * @param int $split_lenght
382. * @return array
383. */
384. function str_split($split_lenght=1){
385. return str_split($this->v,$split_lenght);
386. }
387.
388.
389. /**
390. * splits the string and returns array
391. *
392. * @param string $regex
393. * @param int $limit
394. * @param int $flags
395. * @return array
396. */
397. function preg_split($regex,$limit=-1,$flags=null){
398. return preg_split($regex,$this->v,$limit,$flags);
399. }
400.
401. /**
402. * splits the string and returns array
403. *
404. * @param string $seperator
405. * @param int $limit
406. * @return array
407. */
408. function split($seperator,$limit=-1){
409. return split($seperator,$this->v,$limit);
410. }
411.
412. /**
413. * get md5 of the string
414. *
415. * @return string
416. */
417. function md5(){
418. return md5($this->v);
419. }
420. /**
421. * performs a regular expression search and replace
422. *
423. * @param mixed $pattern
424. * @param mixed $replace
425. * @param int $limit
426. * @param int $count
427. * @return istring
428. */
429. function preg_replace($pattern,$replace,$limit=-1,&$count=null){
430. $this->v = $this->preg_replace_r($pattern,$replace,$limit,$count);
431. return $this;
432. }
433.
434. /**
435. * performs a regular expression search , replace and returns the result
436. *
437. * @param mixed $pattern
438. * @param mixed $replace
439. * @param int $limit
440. * @param int $count
441. * @return mixed
442. */
443. function preg_replace_r($pattern,$replace,$limit=-1,&$count=null){
444. return preg_replace($pattern,$replace,$this->v,$limit,$count);
445. }
446.
447.
448. /**
449. * Performs a regular expression match and returns the result
450. *
451. * @param string $pattern
452. * @param array $matches
453. * @param int $flags
454. * @param int $offset
455. * @return int
456. */
457. function preg_match($pattern,&$matches=null,$flags=null,$offset=null){
458. return preg_match($pattern,$this->v,&$matches,$flags,$offset);
459. }
460.
461.
462.
463. /**
464. * removes javascript elemens and html tags ..
465. *
466. * @return istring
467. */
468. public function clearHtml(){
469. $this->v = $this->clearHtml_r();
470. return $this;
471. }
472.
473. /**
474. * removes javascript elemens , html tags and returns affected data
475. *
476. * @return string
477. */
478. public function clearHtml_r(){
479. $search = array (
480. '@<script[^>]*?>.*?</script>@si', // Strip out javascript
481. '@<[\/\!]*?[^<>]*?>@si' // Strip out HTML tags
482. );
483.
484. $replace = array ('','');
485.
486. return $this->preg_replace_r($search,$replace);
487.
488. }
489.
490.
491. /**
492. * trims data
493. *
494. * @return istring
495. */
496. function trim($removeNewLines=true){
497.
498. $this->v = $this->trim_r($removeNewLines);
499. return $this;
500. }
501.
502. /**
503. * trims data and get affected string
504. *
505. * @return string
506. */
507. function trim_r($removeNewLines=true){
508. $patten = array("/\t/im","/\r/im","/[\s]{2,}/im");
509. $replace = array('','',' ');
510. if ($removeNewLines) {
511. $patten[] = "/\n/im";
512. $replace[] = '';
513. }
514. return preg_replace($patten,$replace,$this->v);
515. }
516.
517. /**
518. * convert all new lines to <br />
519. *@return istring
520. */
521. function nl2br(){
522. $this->v = $this->nl2br_r();
523. return $this;
524. }
525. /**
526. * convert all new lines to <br /> and get the value
527. *
528. * @return string
529. */
530. function nl2br_r(){
531. return nl2br($this->v);
532. }
533.
534. /**
535. * convert all <br /> to new lines
536. *
537. * @return istring
538. */
539. function br2nl(){
540. $this->v = $this->br2nl_r();
541. return $this;
542. }
543.
544. /**
545. * convert all <br[ /]> to new lines and get the value
546. *
547. * @return string
548. */
549. function br2nl_r(){
550. return $this->preg_replace_r("/<br(\s\/)?>/im","\n");
551. }
552.
553. // validations
554.
555.
556.
557. /**
558. * checks whether is empty or not
559. *
560. * @return boolean
561. */
562. function isEmpty(){
563. return empty($this->v);
564. }
565.
566. /**
567. * checks whether is alpha-numeric or not
568. *
569. * @return boolean
570. */
571. function isAlpha(){
572. return (boolean) ereg('^[a-zA-Z0-9çöşüğıÇÖİŞÜĞ]+$',$this->v);
573. }
574.
575. function isUsername(){
576. return (boolean) ($this->isAlpha() && ($this->length() < 12) );
577. }
578.
579. /**
580. * checks whether it is equal testString or it is not
581. *
582. * @param mixed $testString
583. * @param boolean $typeMatching
584. * @return boolean
585. */
586. function isEqual($testString,$typeMatching=false){
587. return ($typeMatching) ? ($testString === $this->v) : ($testString == $this->v);
588. }
589.
590. /**
591. * checks whether it is NOT equal testString or it is
592. *
593. * @param mixed $testString
594. * @param boolean $typeMatching
595. * @return boolean
596. */
597. function isNotEqual($testString,$typeMatching=false){
598. return ($typeMatching) ? ($testString !== $this->v) : ($testString != $this->v);
599. }
600.
601. /**
602. * checks whether is valid email or not
603. *
604. * @return boolean
605. */
606. function isEmail(){
607. return (boolean)
608. eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$",$this->v);
609. }
610.
611. /**
612. * checks whether is numeric or not
613. *
614. * @return boolean
615. */
616. function isNumeric(){
617. return (boolean) is_numeric($this->v);
618. }
619.
620.
621.
622. /**
623. * checks whether is date compliant or not
624. *
625. * @return boolean
626. */
627. function isDate(){
628. if (preg_match('/^\d{9}$/i',$this->v)) {
629. return true;
630. }else{
631. list($year, $month, $day) = sscanf($this->v, '%d-%d-%d');
632. return (boolean) checkdate($month, $day, $year);
633. }
634. }
635.
636. /**
637. * checks whether is alpha-numeric or not
638. *
639. * @return boolean
640. */
641. function isName(){
642. return (bool) !preg_match('/[^[:alpha:]\ \-\']/', $this->v);
643. }
644.
645.
646. }
647.
648.
649. // kullanımı için birkaç örnek
650.
651. /*$_POST['veri'] = '
652. <script type="text/javascript">js kodu </script>
653. <div onlick="jsfunction();" style="margin:4px">text ...</div>';
654. */
655.
656. $str = new istring($_POST['veri']);
657. $str->escapeJavascript()->trim()->_print();
658.
659. // çıktı : <div style="margin:4px">text ...</div>
660.
661.
662. $str = new istring($_POST['veri']);
663. $str->escapeJavascript()->htmlspecialchars()->trim()->_print();
664.
665. // çıktı : &lt;div style=&quot;margin:4px&quot;&gt;text ...&lt;/div&gt;
666.
667.
668. $str2 = new istring('2001-12-02');
669. if ($str2->isDate()) {
670. var_dump($str2->split('-'));
671. }
672. /* çıktı :
673. array(3) {
674. [0]=>
675. string(4) "2001"
676. [1]=>
677. string(2) "12"
678. [2]=>
679. string(2) "02"
680. }
681. */
682.
683.
684. ?>