note 86366 added to function.preg-match-all

From: Date: Wed, 15 Oct 2008 09:56:15 +0000
Subject: note 86366 added to function.preg-match-all
Groups: php.notes 
Request: Send a blank email to php-notes+get-145371@lists.php.net to get a copy of this message
Recently I had to write search engine in hebrew and ran into huge amount of problems. My data was stored in MySQL table with utf8_bin encoding. So, to be able to write hebrew in utf8 table you need to do <?php $prepared_text = addslashes(urf8_encode($text)); ?> But then I had to find if some word exists in stored text. This is the place I got stuck. Simple preg_match would not find text since hebrew doesnt work that easy. I've tried with /u and who kows what else. Solution was somewhat logical and simple... <?php $db_text = bin2hex(stripslashes(utf8_decode($db_text))); $word = bin2hex($word); $found = preg_match_all("/($word)+/i", $db_text, $matches); ?> I've used preg_match_all since it returns number of occurences. So I could sort search results acording to that. Hope someone finds this useful! ---- Server IP: 69.147.83.197 Probable Submitter: 87.116.153.159 ---- Manual Page -- http://www.php.net/manual/en/function.preg-match-all.php Edit -- https://master.php.net/note/edit/86366 Del: integrated -- https://master.php.net/note/delete/86366/integrated Del: useless -- https://master.php.net/note/delete/86366/useless Del: bad code -- https://master.php.net/note/delete/86366/bad+code Del: spam -- https://master.php.net/note/delete/86366/spam Del: non-english -- https://master.php.net/note/delete/86366/non-english Del: in docs -- https://master.php.net/note/delete/86366/in+docs Del: other reasons-- https://master.php.net/note/delete/86366 Reject -- https://master.php.net/note/reject/86366 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#145371) next »