5

I have a text ($txt), an array of words ($words) i want to add a link and a word ($wordToExclude) that must be not replaced.

$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.'

I want to obtain this result:

$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.'
6
  • You need to give us a shortened example; reading all that fake Latin makes my eyes hurt.
    – Bojangles
    Sep 22, 2011 at 12:14
  • @TimPietzcker where does he say that? The way I read it is: "I want to replace these words with a link, as long as those words aren't in the $wordsToExclude", which doesn't make sense seeing as $wordsToExclude would be redudant Sep 22, 2011 at 12:22
  • @TimPietzcker Now I see it... my bad Sep 22, 2011 at 12:22
  • @TimPietzcker the example could have been a lot simpler Sep 22, 2011 at 12:23
  • Or more complex: Could $wordsToExclude also contain something like adipiscing elit or arcu fringilla imperdiet? Parts of words? Etc. You're right, the specifications are hazy right now. Sep 22, 2011 at 12:26

3 Answers 3

3
$result = preg_replace(
    '/\b                 # Word boundary
    (                    # Match one of the following:
     (?<!consectetur\s)  #  (unless preceded by "consectetur "
     adipiscing          #  adipiscing
    |                    # or
     molestie            #  molestie
    |                    # etc.
     fringilla
    )                    # End of alternation
    \b                   # Word boundary
    /ix', 
    '<a href="#">\1</a>', $subject);
2
  • I think he's trying to build a function with 3 args, text, a word array and a word to exclude. He wants the function the replace all the words in the word array but exclude one type of that combination in the text. So it should be general using his variables.
    – NorthGuard
    Sep 22, 2011 at 12:22
  • 1
    @inTide: Yup. Specs are hazy, crystal ball broken. Sep 22, 2011 at 12:27
2

Okie doke! While I think this is technically doable, the solutions I have provided are kind of soft at this point:

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

turns...

sit amet, consectetur adipiscing elit. Quisque... ligula sem molestie massa... nec arcu fringilla imperdiet... nec adipiscing velit.

into...

sit amet, consectetur adipiscing elit. Quisque... ligula sem <a href="#LinkBasedUponmolestie">molestie</a> massa... nec arcu <a href="#LinkBasedUponfringilla">fringilla</a> imperdiet... nec <a href="#LinkBasedUponadipiscing">adipiscing</a> velit.

The reason it is a soft solution is that it does not handle partial words or other cases where the word(s) to exclude do not either begin or end with one of the words to be matched. e.g, if we were to append to the excluded 'word' (i.e. consectetur adipiscing elit), this expression would end up matching the adipiscing in consectetur adipiscing elit, because adipiscing does not begin or end the same as consectetur adipiscing elit

It should work as long as your exclude 'word' (A B C) always ends or begins with one of the words to be found (C|X|E has a C in it, and A B C ends with the word C, so should therefore work...)

EDIT {

The reason the 'not matched' words must begin or end with one of the matched words is that this solution uses negative lookahead before the match, and negative lookbehind after the match to ensure that the matched sequence does not match the words to not be matched (does that make sense?)

}

There are certain solutions to this, but they are either or both processor and programming effort intensive, and get exponentially more so depending on the size of the lists of words and the length of the searched text AND the specific requirements - and you never specified anything else, so I'm not gonna go into it at this point. Let me know if this is good enough for your situation!

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.