Solarpitch Posted March 30, 2011 Share Posted March 30, 2011 Hey Guys, I have a html page and I want to set some markers or tags so when I submit the page, I can strip out content between those tags. For example something like <!-- STRIP --> .. html content la la la.. <-- END STRIP --> or have a div with a class <div class="strip"> ... html content la la la </div> A. Which would be the best one? B. How can I do this with ereg exp? Quote Link to comment Share on other sites More sharing options...
betterphp Posted March 30, 2011 Share Posted March 30, 2011 well if you have the contents of the page as a string then you can use preg_replace $stripped = preg_replace('#<!-- STRIP -->(.*)<-- END STRIP -->#s', '', $string); but if this is actually a php page you can just use an if statement Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted March 30, 2011 Share Posted March 30, 2011 I prefer: <!-- STRIP --> .. html content la la la.. <-- END STRIP --> And this should work: $output = preg_replace('/<--STRIP-->.*?<--END STRIP-->/s', '', $input); Quote Link to comment Share on other sites More sharing options...
Solarpitch Posted March 30, 2011 Author Share Posted March 30, 2011 Thanks guys but neither seem to work.. I tried: $code = preg_replace('/<--STRIP-->.*?<--END STRIP-->/s', '', $this->input->post('content')); $code = preg_replace('#<!--STRIP-->(.*)<--END -STRIP->#s', '', $this->input->post('content')); Very strange.. the content is coming through and when I save the code to the database directly after this call, everything is still inside the tags. Quote Link to comment Share on other sites More sharing options...
betterphp Posted March 30, 2011 Share Posted March 30, 2011 $string = 'Hey Guys, I have a html page and I want to set some markers or tags so when I submit the page, I can strip out content between those tags. For example something like <!-- STRIP --> .. html content la la la.. <-- END STRIP --> or have a div with a class <div class="strip"> ... html content la la la </div> A. Which would be the best one? B. How can I do this with ereg exp?'; $stripped = preg_replace('#<!-- STRIP -->(.*)<-- END STRIP -->#s', '', $string); echo $stripped; This example worked for me, although the ( brackets are not necessary. Can you post your full code ? Quote Link to comment Share on other sites More sharing options...
Solarpitch Posted March 30, 2011 Author Share Posted March 30, 2011 Actually, I'm sending the post using Ajax where I need to escape() the html... $(function() { $(".save_newsletter").click(function() { var id = $(this).attr("id"); var val = escape($("#content").html()); // escape var dataString = 'content='+ val; $.ajax({ type: "POST", url: "<?php echo site_url(); ?>newsletter/save_newsletter/"+id, cache: false, data: dataString, success: function(html){ $('.success').prepend(html); } }); return false; }); }); This could be causing a problem? EDIT: it must be, after it's escaped it sends through as .. 205px%3B%20border%3A%201px%20dashed% etc But appears as normal in the database after the post and insert. Quote Link to comment Share on other sites More sharing options...
betterphp Posted March 30, 2011 Share Posted March 30, 2011 well you could use the escaped versions of < and > in the regex, and what ever else it changes of course. Quote Link to comment Share on other sites More sharing options...
Solarpitch Posted March 30, 2011 Author Share Posted March 30, 2011 <!--STRIP--> escaped as %0A%3C%21--STRIP--%3E%0A So like this... $code = preg_replace('#%0A%3C%21--STRIP--%3E%0A(.*)%0A%3C%21--ENDSTRIP--%3E%0A#s', '', $this->input->post('reply_content')); Quote Link to comment Share on other sites More sharing options...
Solarpitch Posted March 30, 2011 Author Share Posted March 30, 2011 That doesnt work either actually. Quote Link to comment Share on other sites More sharing options...
betterphp Posted March 30, 2011 Share Posted March 30, 2011 if you do echo $this->input->post('reply_content'); that is what you need to be working with. Also can you post the full thing ?? Quote Link to comment Share on other sites More sharing options...
Solarpitch Posted March 30, 2011 Author Share Posted March 30, 2011 Here's the code... Save - submit call to Ajax <a href="#" class="save_newsletter" id="<?php echo $template_id; ?>">Save</a> Ajax - get html from <div class="content"></div> $(function() { $(".save_newsletter").click(function() { var id = $(this).attr("id"); var val = escape($("#content").html()); var dataString = 'content='+ val; $.ajax({ type: "POST", url: "<?php echo site_url(); ?>newsletter/save_newsletter/"+id, cache: false, data: dataString, success: function(html){ $('.success').prepend(html); } }); return false; }); }); HTML - that was sent to the Ajax function from the content div with the tags <!--STRIP--> <div id="product_list" style="display: none;"> <div style="width: 100%; float: left; font-family: Arial; font-size: 12px; padding: 5px; border: 1px dashed rgb(204, 204, 204); margin-bottom: 10px;"> <div style="float: left; width: 200px;">23%u201D Automatic Umbrella</div> <div style="float: right; width: 150px;"><a href="#" class="link" id="25_3">Add to Newsletter</a></div> </div> <div style="width: 100%; float: left; font-family: Arial; font-size: 12px; padding: 5px; border: 1px dashed rgb(204, 204, 204); margin-bottom: 10px;"> <div style="float: left; width: 200px;">A Ballylanders Rebel</div> <div style="float: right; width: 150px;"><a href="#" class="link" id="82_3">Add to Newsletter</a></div> </div> <div style="width: 100%; float: left; font-family: Arial; font-size: 12px; padding: 5px; border: 1px dashed rgb(204, 204, 204); margin-bottom: 10px;"> <div style="float: left; width: 200px;">Automatic Umbrella (Tiger Woods)</div> <div style="float: right; width: 150px;"><a href="#" class="link" id="31_3">Add to Newsletter</a></div> </div> <div style="width: 100%; float: left; font-family: Arial; font-size: 12px; padding: 5px; border: 1px dashed rgb(204, 204, 204); margin-bottom: 10px;"> <div style="float: left; width: 200px;">Black Labrador Soft Toy</div> <div style="float: right; width: 150px;"><a href="#" class="link" id="52_3">Add to Newsletter</a></div> </div> <div style="width: 100%; float: left; font-family: Arial; font-size: 12px; padding: 5px; border: 1px dashed rgb(204, 204, 204); margin-bottom: 10px;"> <div style="float: left; width: 200px;">Children%u2019s Baking Set</div> <div style="float: right; width: 150px;"><a href="#" class="link" id="14_3">Add to Newsletter</a></div> </div> </div> <!--ENDSTRIP--> PHP - Get posted code from Ajax and save to DB public function save_newsletter() { $code = preg_replace('#<!--STRIP-->(.*)<--ENDSTRIP-->#s', '', $post); $insert = array( 'code' => $code, 'client_id' => $this->session->userdata('client_id'), 'client_uri' => $this->session->userdata('client_uri'), ); $this->Newsletter_model->save_newsletter($insert); } Quote Link to comment Share on other sites More sharing options...
betterphp Posted March 30, 2011 Share Posted March 30, 2011 you have <!--ENDSTRIP--> in your html but <--ENDSTRIP--> in the reg exp. Quote Link to comment Share on other sites More sharing options...
Solarpitch Posted March 30, 2011 Author Share Posted March 30, 2011 Ah well spotted mate. Thanks for your help. Works now. Quote Link to comment Share on other sites More sharing options...
betterphp Posted March 30, 2011 Share Posted March 30, 2011 Thanks for your help. no problem Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.