$10 for solving regex issue

Discussion in 'PHP' started by DarkAge, Dec 28, 2008.

  1. #1
    I need to replace some keywords in some text which are enclosed with <p></p>. These keywords will be replaced with links.

    To outline the rules:
    1. Replace only keywords that are within a <p> and </p>
    2. Replace only keywords that are NOT with a <a> and </a>

    This is what I got (which performs rule 1):
    
    $patterns[$counter] = "/(<p>.*\b)(".$keyword.")(\b.*<\/p>)/isU";//   
    $replacements[$counter] = "$1<a title=\"".$anchorText."\" href=\"".$url."\">$2</a>$3";
    .
    .
    .
    $pageContent = preg_replace($patterns, $replacements, $pageContent);
    PHP:
    However I need to modify the regex patter to include rule 2.

    Examples of how this should work for keyword 'piston' and 'cylinder':

    It should match this in bold

    It should NOT match this in bold
    It should match this in bold
    It should match this in bold
    $10 to the person who can provide the correct regex
     
    DarkAge, Dec 28, 2008 IP
  2. nitsanbn

    nitsanbn Active Member

    Messages:
    382
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    58
    #2
    add me to msn i will help you with this for free ;)
     
    nitsanbn, Dec 28, 2008 IP
  3. DarkAge

    DarkAge Active Member

    Messages:
    353
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    58
    #3
    bump back up
     
    DarkAge, Dec 29, 2008 IP
  4. apsam29

    apsam29 Peon

    Messages:
    68
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I think you are trying to create SEO optimization.
    Just create a inner expression for your pattern code,
    just creat a nested condition inside $keyword like , (....)
     
    apsam29, Dec 30, 2008 IP
  5. mallorcahp

    mallorcahp Peon

    Messages:
    141
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I would try something like this as your pattern:

    or maybe don't need all the "(" and ")" so could be simpler :

     
    mallorcahp, Dec 30, 2008 IP
  6. DarkAge

    DarkAge Active Member

    Messages:
    353
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    58
    #6
    The first part of the regex would match keywords within a link as wel messing up the html.

    Anyway, I managed to solve it myself by breaking the regex up into several checking for all combinations of no link, link before, link after, link before and after keyword within a paragraph

    Question closed.
     
    DarkAge, Dec 31, 2008 IP