Regex - replace @s and .s between tags.

Discussion in 'PHP' started by blueparukia, May 25, 2008.

  1. #1
    I have a snippet in a message, something like this:

    
    [message]sometext.moretext@.text[message]
    
    Code (markup):
    I want that to output as:

    I can not comprehend the regex I would have to use to get that working, since it must only work between [message]and[/message] tags.

    Thanks,

    BP
     
    blueparukia, May 25, 2008 IP
  2. vtida.com

    vtida.com Peon

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    $text="[message]sometext.moretext@.text[message]";

    preg_match("/\[message\](.*?)\[message\]/i",$text,$result);

    $mix_search= array('.', '@');
    $mix_replace= array('[dot]', '[at]');

    $output = str_replace($mix_search, $mix_replace, $result[1]);

    Finish
     
    vtida.com, May 25, 2008 IP
  3. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #3
    
    <?php
    function friendly_emails( $text )
    {
    	if( preg_match_all( '~\[message\](.[^\[]+)\[/message]~si', $text, $emails ) > 0 )
    	{
    		for( $i = 0; $i < count( $emails[0] ); $i++ )
    		{
    			$text = str_replace
    			( 
    				$emails[0][$i],
    				preg_replace
    				( 
    					array( '~@~', '~\.~' ),
    					array( '[at]', '[dot]' ),
    					$emails[1][$i]
    				),
    				$text
    			);
    		}
    	}
    	return $text ;
    }
    
    $document =<<<HEREDOC
    email me @ [message]my@email.com[/message], .'s outside of [message] tag not touched ...
    HEREDOC;
    
    echo friendly_emails( $document );
    ?>
    
    PHP:
     
    krakjoe, May 25, 2008 IP
    blueparukia likes this.