need help with preg_replace

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

  1. #1
    I've got articles that contain certain words I'd like to replace with links.

    These words are between <p></p> tags. The word is identified by having a space in front of it and after it. I only want to replace the word with the link.

    This is what I have done so far (the test word is hello), but it doesn't work:

    
    $patterns[0] = "/<p>.+\s(hello)\s.+<\/p>/i";
    $replacements[0] = "AAA"; // for testing
    $pageContent = preg_replace($patterns, $replacements, $pageContent);	
    
    Code (markup):
    Thanks in advance.
     
    DarkAge, Dec 25, 2008 IP
  2. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #2
    Eww, regex

    <?php
    
    $example_text = <<<txt
    I've got articles that contain certain words I'd like to replace with links.
    These words are between <p></p> tags. The word is identified by having a space in front of it and after it. I only want to replace the word with the link.
    This is what I have done so far (the test word is hello), but it doesn't work:
    txt;
    /* Just an example */
    
    $replace = array(
    'articles' => 'http://articles.com',
    'got' => 'http://google.com',
    );
    /* Rename what to where */
    
    array_walk($replace, create_function('&$value, $key', '$value = \'<a href="\' . $value . \'">\' . $key. \'</a>\';'));
    $out = strtr($example_text, $replace);
    /* The important bit */
    
    echo $out;
    PHP:
     
    Danltn, Dec 26, 2008 IP
  3. DarkAge

    DarkAge Active Member

    Messages:
    353
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    58
    #3
    hmm interesting approach, ill look into that. So far I've got this:

    $patterns[0] = "/(<p>[^<]*\b)(hello)(\b[^>]*<\/p>)/isU";

    However I have an issue though with negating the '<' and '>'. I cant replace multiple keywords with links within the same paragraph as once the first replacement is done, it now contains <a href="" ... so all other matches on that paragraph fails.

    You're solution might be better according to the KISS principle, but what about performance ?

    Edit: I don't see your code checking for keywords only within <p> and </p>. This code could replace non text words, such as javascript or html element names (due to the large volume of keywords and their content)
     
    DarkAge, Dec 26, 2008 IP
  4. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #4
    Surely you have a hook or similar to just make it run on the text that needs changing?

    Performance wise it's better, as it doesn't need to use the RegEx engine. (RegEx is slow, especially with . modifiers. - and anyway, performance in PHP is negligible. With so many functions in global scope everything is slow anyway.
     
    Danltn, Dec 26, 2008 IP