How to remove/replace/delete all text after a certain string

Discussion in 'PHP' started by Brinked, Nov 12, 2007.

  1. #1
    I am trying to remove all text after the phrase "(paste this code" within a variable.

    I understand preg_replace can do this but how?
     
    Brinked, Nov 12, 2007 IP
  2. decepti0n

    decepti0n Peon

    Messages:
    519
    Likes Received:
    16
    Best Answers:
    0
    Trophy Points:
    0
    #2
    echo substr($str, 0, stripos($str, "(paste this code"));
    PHP:
    Should work, unless you wanted to include the paste this code bit
     
    decepti0n, Nov 12, 2007 IP
  3. Brinked

    Brinked Well-Known Member

    Messages:
    777
    Likes Received:
    51
    Best Answers:
    0
    Trophy Points:
    160
    #3
    thanks deception..unfortunatley, i dont have php5 on my server, so it gives me a function undefined.
     
    Brinked, Nov 12, 2007 IP
  4. heroic

    heroic Peon

    Messages:
    252
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #4
    u could use preg_match to find the string after the paste this thing...
     
    heroic, Nov 12, 2007 IP
  5. Brinked

    Brinked Well-Known Member

    Messages:
    777
    Likes Received:
    51
    Best Answers:
    0
    Trophy Points:
    160
    #5
    the content after the Paste string is always different, otherwise i would just use str_replace on that as well. unless im misunderstanding out preg_match works.
     
    Brinked, Nov 12, 2007 IP
  6. heroic

    heroic Peon

    Messages:
    252
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #6
    with preg_match you can get any string with paste this behind...
     
    heroic, Nov 12, 2007 IP
  7. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #7
    
    <?php
    /**
     * @author Interviolet
     * @package [$package]
     * @filename [$filename]
     * @copyright 2007
     */
    
    function clean_phrase( $phrase )
    {
    	if( preg_match( "~(\(paste this code)(.*)~", $phrase, $clean ) )
    	{
    		return str_replace( $clean[2], "", $phrase );
    	}
    }
    function clean_phrase2( $phrase )
    {
    	if( preg_match( "~(\(paste this code)(.*)~", $phrase, $clean ) )
    	{
    		return substr( $phrase, 0, strpos( $phrase, $clean[2] ) );
    	}
    }
    
    $phrase = "(paste this code some more text here";
    
    echo clean_phrase( $phrase );
    echo "<br />\n";
    echo clean_phrase2( $phrase );
    ?>
    
    PHP:
     
    krakjoe, Nov 13, 2007 IP
  8. MMJ

    MMJ Guest

    Messages:
    460
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #8
    If thats really all you need :

    <?php
    $str = '(paste this code' . 'this will be stripped';
    substr($str, 0, strlen('(paste this code'))
    ?>
    PHP:
     
    MMJ, Nov 13, 2007 IP
  9. Brinked

    Brinked Well-Known Member

    Messages:
    777
    Likes Received:
    51
    Best Answers:
    0
    Trophy Points:
    160
    #9
    thanks for the help guys, got it to work with this:

    echo(substr($content1, 0, strpos($content1, '(paste this code')));
     
    Brinked, Nov 13, 2007 IP