English 中文(简体)
PHP 在排除一个特定词的情况下,可与词句相对应。
原标题:PHP Regex match words in a string excluding one specific word

我有一份案文(txt),一句话(序号)希望添加一个链接和一个字(前言)而不必替换。

$words = array ( adipiscing , molestie , fringilla );
$wordToExclude =  consectetur adipiscing ;


$txt =  Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque
mattis tincidunt dolor sed consequat. Sed rutrum, mauris convallis bibendum 
dignissim, ligula sem molestie massa, vitae condimentum neque sem non tellus.
Aenean dolor enim, cursus vel sodales ac, condimentum ac erat. Quisque
lobortis libero nec arcu fringilla imperdiet. Pellentesque commodo, 
arcu et dictum tincidunt, ipsum elit molestie ipsum, ut ultricies nisl
neque in velit. Curabitur luctus dui id urna consequat vitae mattis
turpis pretium. Donec nec adipiscing velit. 

我获得这一结果:

$txt =  Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque
mattis tincidunt dolor sed consequat. Sed rutrum, mauris convallis bibendum 
dignissim, ligula sem <a href="#">molestie</a> massa, vitae condimentum neque sem non tellus.
Aenean dolor enim, cursus vel sodales ac, condimentum ac erat. Quisque
lobortis libero nec arcu <a href="#">fringilla</a> imperdiet. Pellentesque commodo, 
arcu et dictum tincidunt, ipsum elit <a href="#">molestie</a> ipsum, ut ultricies nisl
neque in velit. Curabitur luctus dui id urna consequat vitae mattis
turpis pretium. Donec nec <a href="#">adipiscing</a> velit. 
问题回答
$result = preg_replace(
     /                 # Word boundary
    (                    # Match one of the following:
     (?<!consecteturs)  #  (unless preceded by "consectetur "
     adipiscing          #  adipiscing
    |                    # or
     molestie            #  molestie
    |                    # etc.
     fringilla
    )                    # End of alternation
                       # Word boundary
    /ix , 
     <a href="#">1</a> , $subject);

Okie doke! 虽然我认为这在技术上是可行的,但我提供的解决办法目前是软性的:

s%(?!consectetur adipiscing)(adipiscing|molestie|fringilla)(?<!consectetur adipiscing)%<a href="#LinkBasedUpon$1">$1</a>%s

页: 1

页: 1 Quisque... ligula sem molestie massa... nec arcu fringilla imperdiet... nec adipiscing velit.

页: 1

坐在金属、sect、adi。 Quisque... ligula sem >,><a href=“#LinkBasedUponmolestie”>molestie<a> Massa... nec arcuong> <>>>>><a href=#LinkedUponfringilla>>fringilla>>></a>/strong>.

软性解决办法是,它不处理部分字或排除(s)一词时,不开始或最后加上一个相应的字句。 e.g,如果我们想读到所排除的字句(即consectetur adipiscing elit),那么这一表述将最终与adipiscingconsectetur adipiscing elit上的匹配。 与<代码>conetur adipiscing el不开始或结束。

只要您的除外词(A B)永远结束或开始使用所发现的词语之一(C>>> /code>C,以及A B CC为终止,就应当做到......)

<><><><>>>>

不匹配的词句必须开始或结束,其原因之一是,这种解决办法在配对前使用消极的头盔,在配对后出现负面的眼光,以确保相匹配的顺序与不匹配的字数不相匹配(有意思的话)

}

有一些解决办法,但是,它们要么是进程,要么是方案拟定工作紧张,而且根据文字清单的规模、搜索案文的长度和具体要求,其成倍增加,而且你从未具体指明其他内容,因此,我不会gon。 让我知道,这是否足以使你的处境!!

I see you re doing it in PHP. I understand you have an ARRAY of words to find in a text and you need to replace those with links. Also you have ONE string that needs to be excluded when doing the replacing. Maybe instead of writing cool and clean yet complicated regular expressions what about this practical albeit probably not the nicest solution:

You split the task into subtasks:

  1. use preg_match_all to find offsets of all occurrences of the excluded string (you know the string length (strlen) and with the PREG_OFFSET_CAPTURE flag for preg_match_all you will figure out exact starts and ends - if there are more than one)
  2. do foreach on your word list and again use preg_match_all to get all occurrences of the words you need to replace with links
  3. compare the positions you found in step 2 with those found in step 1 and if they re outside do the replace or skip if you get overlap

It surely won t be a one-liner but would be quite easy to code and then probably quite easy to read later too.





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签