За последние 24 часа нас посетили 94742 программиста и 5606 роботов. Сейчас ищут 2439 программистов ...

Поиск ключевых фраз в тексте

Тема в разделе "Прочие вопросы по PHP", создана пользователем pr0n1x, 16 май 2011.

  1. pr0n1x

    pr0n1x Активный пользователь

    С нами с:
    30 мар 2006
    Сообщения:
    486
    Симпатии:
    2
    Адрес:
    Киев
    Здравствуйте, есть список фраз, например:

    1. de Finibus Bonorum et Malorum
    2. random text
    3. The Extremes of Good and Evil
    4. latin words

    и есть текст:

    HTML:
    1. Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.
    Мне нужно, чтобы скрипт нашел в тексте все совпадения, регистронезависимые и выделил их жирным шрифтом.
    Может есть у кого, уже готовые решения?
     
  2. asik

    asik Активный пользователь

    С нами с:
    12 июл 2007
    Сообщения:
    211
    Симпатии:
    0
    PHP:
    1.  
    2. <?php
    3.   $search=array('de Finibus Bonorum et Malorum','random text');
    4.   $replace=array('<b>de Finibus Bonorum et Malorum</b>','<b>random text</b>');
    5.   $text=' Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.';
    6.   $answer = str_ireplace($search,$replace,$text);
    7. echo $answer;
    8.  
    9.  
     
  3. pr0n1x

    pr0n1x Активный пользователь

    С нами с:
    30 мар 2006
    Сообщения:
    486
    Симпатии:
    2
    Адрес:
    Киев
    А если количество искомых слов будет больше 5000, этот скрипт нужно как-то оптимизировать или str_ireplace сделает это автоматически?