Hey everybody !

I got this script which searchs on a data base for a word and returns me results, thats not a problem, then on PHP I'd like to mark in bold every word match I get in the results.

However if I search for the word 'menu' im going to get some results from mysql with the word 'menú' (notice the accent)

My usual algorithm to bold this would be

$find = 'menu';
$string = '.... in other languages we say menú asd ...';
$part1 = substr($string, 0, strpos( strtolower($string) ,strtolower($find)));
$part2 = substr($string, strpos(strtolower($string),strtolower($find)),strlen($find));
$part3 = substr($string,strpos(strtolower($string),strtolower($find))+strlen($find));

$boldedString = $part1 . "<b>".$part2."</b>".$part3;

How can I use strpos to match 'menú' if im searching for 'menu' :/ ??

I already tryed replacing the accents with googled functions, and mb_strpos doesnt seemz to work :/

    4 days later

    I'm not certain, but I don't really think strpos will handle this correctly because the u with an accent is literally a different character than the u without an accent -- at least as far as strpos can tell. In a given charset, the one with an accent has a different ordinal than the one without.

    You might want to try preg_match. It's much more powerful. To bold a given word, you could try this:

      GNU nano 2.0.2                                        File: chump.php                                                                            Modified
    
    <?php
    
    $string = '.... in other languages we say men&#239;&#191;&#189; asd ...'; // the string to be searched
    $find = 'menu'; // the word to be bolded
    
    $pattern = '/(men(u|&#239;&#191;&#189;))/i';
    
    $boldedString = preg_replace($pattern, '<b>$1</b>', $string);
    
    echo $boldedString;
    
    ?>
    

    NOTE that you need to declare a charset in your document or you'll get some funny-looking characters instead of the accented U you seek.

    I realize that you can't easily feed any arbitrary word into this code because it is custom-written for the menu example you gave. It might be possible to come up with some fairly involved code that properly generates a replacement pattern for any accented characters or their unaccented equivalents.

      Write a Reply...