Buying PHP developer who can help me build regex - $10

Discussion in 'Programming' started by DarkAge, Aug 9, 2009.

  1. #1
    I need a regex that can replace certain words in forum posts with links (already have this regex):
    
    eregi_replace("(word)", "[url=http://link]\\1[/url]", $postText);
    
    PHP:
    However it should not replace words that are already in url bbcode.
    Eg dont replace if:
    
    [url=http://www.google.com]word[/url]
    [url]word[/url]
    
    PHP:
    Create the regex and ill paypal you $10.
     
    DarkAge, Aug 9, 2009 IP
  2. Demonic

    Demonic Active Member

    Messages:
    821
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    85
    As Seller:
    100% - 0
    As Buyer:
    100% - 0
    #2
    Just use str_replace

    <?php
    $postText = str_replace($word, "[url=http://blah]{$word}[/url]", $postText);
    ?>
    PHP:
    To do it with array:


    <?php
    $words = array('word' => 'http://link', 'word' => 'http://link');
    foreach($words as $word => $link)
    {
    $postText = str_replace($word, "[url={$link}]{$word}[/url]", $postText);
    }
    ?>
    PHP:
    If you want me to integrate this into your script contact me. I'll do it the way you want via PM me if interested.
     
    Demonic, Aug 9, 2009 IP
  3. luckymurari

    luckymurari Active Member

    Messages:
    629
    Likes Received:
    40
    Best Answers:
    0
    Trophy Points:
    90
    As Seller:
    100% - 0
    As Buyer:
    100% - 0
    #3
    Your code still doesn't solve op'c prob of not replacing the words wrapped by url tags..

    Hi, DarkAge.. here is the code that doies your task. I have sent you my paypal for payment.

    <?php 
    $html="testing word xyz [url=http://worst]word[/url] ";
    
    $strParts = preg_split( '/(word)/', $html,-1,PREG_SPLIT_DELIM_CAPTURE);
    
        foreach( $strParts as $key=>$part ) {
            if( $part=="word"  && !(preg_match('/\[url=(.*?)\]$/',$strParts[$key-1] )) ) {
    		
    		$strParts[$key] = preg_replace( '/word/', '[url=http://replaced]word[/url]', $part );
            }
    
        }
        $html = implode( $strParts );
    	
    echo $html;
    	?>
    PHP:
     
    luckymurari, Aug 9, 2009 IP
  4. stOK

    stOK Active Member

    Messages:
    114
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    53
    As Seller:
    100% - 0
    As Buyer:
    100% - 0
    #4
    It's not a regex. It's procedure. More over it fails the following test

    
    $html="testing word xyz [url=http://worst]word blabla word[/url] ";
    
    PHP:
     
    stOK, Aug 9, 2009 IP
  5. luckymurari

    luckymurari Active Member

    Messages:
    629
    Likes Received:
    40
    Best Answers:
    0
    Trophy Points:
    90
    As Seller:
    100% - 0
    As Buyer:
    100% - 0
    #5
    I can say in confirmity that no1 can do it in single regex and this is the standard procedure for it.. and with regards to that test.. u were only saying u wanted "word" wrapped by url not to be re-evaluated.. so I din't take that case.. neways.. if u need the soln in this way, I can get it for that case too... and I can assert you, no1 in the world can do that in single regex
     
    luckymurari, Aug 9, 2009 IP
  6. DarkAge

    DarkAge Active Member

    Messages:
    353
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    58
    As Seller:
    100% - 0
    As Buyer:
    100% - 0
    #6
    stOK, your regex got me closer.

    I've modified it to this:
    
    $postText = preg_replace('{(word)(?![^\[\]]*\[/url\])}', "REPLACED", $postText);
    
    PHP:
    (REPLACED) is just so I can see what it is replacing.
    I removed the check for whitespace before and after as it would not match the word at the beginning or end of the post.

    However I still have 2 issues:
    1. It's case sensitive where it should be insensitive
    2.
    
    [url=http://www.google.com]word[/url] - OK
    [url]word[/url] - OK
    [url=http://www.word.com]blabla[/url] - FAILED (didnt include that in my op)
    
    PHP:
    It shouldnt match word in the last URL bbcode. Can you help me with the 2 issues ?
     
    DarkAge, Aug 10, 2009 IP
  7. mad_gundam

    mad_gundam Peon

    Messages:
    30
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    As Seller:
    100% - 0
    As Buyer:
    100% - 0
    #7
    i think this is what you want please check
    preg_replace('/(word)(?!\[\/url\])/iU', 'REPLACE', '[url=http://www.google.com]word[/url] word ');
    PHP:
     
    mad_gundam, Aug 11, 2009 IP