SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Thread: Replace

  1. #1
    Put your best practices away. The New Guy's Avatar
    Join Date
    Sep 2002
    Location
    Canada
    Posts
    2,087
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)

    Replace

    I need this.

    $string = 'something'
    $string2 = something end'
    $text = 'huge amount of text'

    I need two functions, one that will delete everything before $string in $text, the other will delete everything after $string 2

    Basicly I want to grab all text in $text between $string and string 2

    Thanks
    "A nerd who gets contacts
    and a trendy hair cut is still a nerd"

    - Stephen Colbert on Apple Users

  2. #2
    SitePoint Wizard Chris82's Avatar
    Join Date
    Mar 2002
    Location
    Osnabrück
    Posts
    1,003
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi.

    PHP Code:
    $string1 'start';
    $string2 'end';

    $text 'blabla start. Everything from here on will lead to the end of it. 
    The problem with the word end is that it now appears twice in this text.'
    ;

    // use this if $string and $string2 appear exactly
    // once in the $text

    preg_match("#$string1(.+?)$string2#is"$text$matches);
    if (
    count($matches) > 0)
        
    $matches $matches[1]; 

    print_r($matches);

    echo 
    '<hr>';

    // use this if $string2 appears several times in $text
    // this matches everything between the first 
    // occurrence of $string1 and the last of $string2

    preg_match("#$string1(.*)$string2#is"$text
    $matches);
    if (
    count($matches) > 0)
        
    $matches $matches[1]; 

    print_r($matches);
      
    ?> 

  3. #3
    Put your best practices away. The New Guy's Avatar
    Join Date
    Sep 2002
    Location
    Canada
    Posts
    2,087
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Worked like a charm, thanks
    "A nerd who gets contacts
    and a trendy hair cut is still a nerd"

    - Stephen Colbert on Apple Users


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •