How to remove a line from a string?

Discussion in 'PHP' started by Leo727, Sep 13, 2006.

  1. #1
    Hi,

    I am trying to figure out how to remove a couple of lines from strings. The lines that need to be removed are not always the same - the text at the start of the lines is always the same, but the end of the lines is always different.

    The string $html contains all the code for an html page, and I just need to remove the 2 lines starting with <meta name="description" and <meta name="keywords"

    For example :

    <html>
    <head>
    [B]<meta name="description" content="example stuff, example, stuff"/> [COLOR="Red"](should be removed)[/COLOR]
    <meta name="keywords" content="example stuff, example, stuff"/> [COLOR="Red"](should be removed)[/COLOR]
    [/B]<meta name="author" content="blah" />
    <meta name="copyright" content="blah" />
    </head><body>Blah</body></html>
    
    Code (markup):
    I need to completely remove the meta description and keyword lines, but leave the rest intact. If anybody could provide the code for this, it would be much appreciated.

    Thanks,
    Leo
     
    Leo727, Sep 13, 2006 IP
  2. wmtips

    wmtips Well-Known Member

    Messages:
    601
    Likes Received:
    70
    Best Answers:
    1
    Trophy Points:
    150
    #2
    $html = preg_replace ('/<meta\s+name="description.*?>/si','',$html);
    $html = preg_replace ('/<meta\s+name="keywords.*?>/si','',$html);
    PHP:
     
    wmtips, Sep 13, 2006 IP
    Leo727 likes this.
  3. tyggemannen

    tyggemannen Guest

    Messages:
    842
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I wish I knew how to put that to use.
     
    tyggemannen, Sep 13, 2006 IP
  4. Leo727

    Leo727 Active Member

    Messages:
    161
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    60
    #4
    Thank You SO much. It works perfectly.
     
    Leo727, Sep 13, 2006 IP
  5. tyggemannen

    tyggemannen Guest

    Messages:
    842
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #5
    A1SiteNames.com , would you please share the complete script you are using for this ? I have to do the exact same thing and as I dont know php I am clueless.
     
    tyggemannen, Sep 13, 2006 IP
  6. Leo727

    Leo727 Active Member

    Messages:
    161
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    60
    #6
    Hi, sorry I used this to modify a commercial script that I bought, so I can't share it. But it's very simple to do this, so long as you already have the string in your script - then you can probably just place those 2 lines after the string is created. Of course you should change the $html at the start and end of each line to the name of the string you are working with.
     
    Leo727, Sep 14, 2006 IP
  7. tyggemannen

    tyggemannen Guest

    Messages:
    842
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #7
    I am so new to php so I really dont know what you are talking about. Thanks anyway : )
     
    tyggemannen, Sep 14, 2006 IP
  8. wmtips

    wmtips Well-Known Member

    Messages:
    601
    Likes Received:
    70
    Best Answers:
    1
    Trophy Points:
    150
    #8
    chowbow, the test code is simple. You need a file test.htm in the same directory:
    
    <?
    $html = file_get_contents('test.htm'); //you can also supply url here
    $html = preg_replace ('/<meta\s+name="description.*?>/si','',$html);
    $html = preg_replace ('/<meta\s+name="keywords.*?>/si','',$html);
    echo $html;
    ?>
    
    PHP:
    Everything else depends on your needs.
     
    wmtips, Sep 14, 2006 IP
    tyggemannen likes this.
  9. tyggemannen

    tyggemannen Guest

    Messages:
    842
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #9
    I understand that it is simple (if you know how to do it :)

    Should the test.htm be empty ?
    And should I just then run test.php in the same directory and the meta tags will disappear ?


    And yes, now you can laugh of my naive questions hehe.
     
    tyggemannen, Sep 14, 2006 IP
  10. wmtips

    wmtips Well-Known Member

    Messages:
    601
    Likes Received:
    70
    Best Answers:
    1
    Trophy Points:
    150
    #10
    chowbow, :)
    I tested this example with contents provided by A1SiteNames.com as an example and saved as file test.htm. This code just reads contents of test.htm (you can also supply url or other filepath instead of 'test.htm'). After trimming meta description and keywords results will be outputted by echo. I.e. if you save this script as test.php and navigate your browser to http://yoursite/test.php, you'll see the contents with this tags stripped. This code does not change the input file, it's just handle it in memory and outputs it. If you need to save result, you can use an easy function file_put_contents:
    file_put_contents('result.htm',$html);
    PHP:
     
    wmtips, Sep 14, 2006 IP
  11. emuut

    emuut Peon

    Messages:
    215
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #11
    thats great. this is what I'm talking about :D
     
    emuut, Sep 14, 2006 IP
  12. tyggemannen

    tyggemannen Guest

    Messages:
    842
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #12
    I must have missed something, just what did you talk about again?
     
    tyggemannen, Sep 14, 2006 IP
  13. tyggemannen

    tyggemannen Guest

    Messages:
    842
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #13
    Thanks wmtips, is file_put_contents('result.htm',$html); only working in php5?
    Anyway, I have so many thins to do now and cannot straighten my head out to learn this. But I need this so I will save this thread and learn it in the near future, so your time is really appreciated.
     
    tyggemannen, Sep 14, 2006 IP
  14. wmtips

    wmtips Well-Known Member

    Messages:
    601
    Likes Received:
    70
    Best Answers:
    1
    Trophy Points:
    150
    #14
    Oh, yes, I forgot to mention, PHP 5 only. I have php 5. But on php 4 you can define substitution (see User Contributed Notes)
     
    wmtips, Sep 14, 2006 IP