1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

String Manipulation Question

Discussion in 'PHP' started by ChaosFoo, Apr 7, 2008.

  1. #1
    I am working on a project to create PDF's from HTML code. I am using DOMpdf to do the creation, and it works perfectly, but I do have a question about preparing the HTML code to be sent to DOMpdf.

    If I have a variable, and it contains a string that is HTML code. How can I remove certain parts of that string? I know it is confusing, but here is an example.

    Here is a sample of the HTML that I am working with:
    
    
    <h1>This is a test page</h1>
    
    <!--remove start-->
    While the comb is open, you place the comb through the holes and close the plastic comb.<br><br>
    <!--remove end-->
    
    <p>...more text down here.</p>
    
    Code (markup):
    I want to remove all of the text beginning with <!--remove start--> and ending with <!--remove end-->. The problem I am having is that the text inside these 2 html comments is dynamic.

    Is there anyway to remove that text using PHP?
     
    ChaosFoo, Apr 7, 2008 IP
  2. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #2
    ereg_replace is my personal favourite.

    $string = ereg_replace('<!--removestart-->.*<!--removeend-->', '', $string);

    Something along that lines, eregi_replace or the perl regex functions can also do this.
     
    Danltn, Apr 7, 2008 IP
    ChaosFoo likes this.
  3. ChaosFoo

    ChaosFoo Peon

    Messages:
    232
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks. Worked like a champ!
     
    ChaosFoo, Apr 7, 2008 IP
  4. Free Directory

    Free Directory Peon

    Messages:
    89
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #4
    or, you can use a php simple function: strip_tags
    string strip_tags ( string str [, string allowable_tags] )


    This function tries to return a string with all HTML and PHP tags stripped from a given str. It uses the same tag stripping state machine as the fgetss() function.

    You can use the optional second parameter to specify tags which should not be stripped.

    Note: allowable_tags was added in PHP 3.0.13 and PHP 4.0b3.
     
    Free Directory, Apr 8, 2008 IP
  5. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #5
    The preg_* functions are supposed to be alot more efficient than the ereg_* functions.

    $string = preg_replace('~<!--removestart-->.*<!--removeend-->~Usi', '', $string);
    Code (markup):
     
    joebert, Apr 8, 2008 IP