Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

preg_replace

Posted on 2011-03-11
8
Medium Priority
?
307 Views
Last Modified: 2012-05-11
For the life of me i can´t figure this out.


I have a html page that needs to exclude some parts in php


html page


<!-- begin part1 -->

Something text here

<!-- end part1 -->

<!-- begin part2 -->

Something text here

<!-- end part2 -->

<!-- begin part3 -->

Something text here

<!-- end part3 -->




php


first i get the html page then i want to exclude a part. Lets say part 2.

I know it's something small but i cannot find anything on the matter.



$new_html = preg_replace("/<!--begin part2 -->(.*)<!-- end part2 -->/","",$old_html);

echo $new_html;

Open in new window

0
Comment
Question by:ScottNL1
  • 4
  • 3
8 Comments
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 35108368
add a space
<!--begin part2 -->
to
<!-- begin part2 -->

So the result:

$new_html = preg_replace("/<!-- begin part2 -->(.*)<!-- end part2 -->/","",$old_html);
echo $new_html;
0
 
LVL 1

Author Comment

by:ScottNL1
ID: 35108571
This is just a typing error on my question. Still don't work.
0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 35108691
And this ?
$new_html = preg_replace("/<!-- begin part2 -->(\n|\r\n)(.*)(\n|\r\n)<!-- end part2 -->/","",$old_html);
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:ScottNL1
ID: 35108761
no just tryed it, founds something as well.

<!-- begin part2 -->something<!-- end part2 -->

If it in the document like this it works.


Like this it doesn't work.
<!-- begin part2 -->

something

<!-- end part2 -->
0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 35108913
Well,
$new_html = preg_replace("/<!-- begin part2 -->(\n\n|\r\n\r\n+)(.*)(\n\n|\r\n\r\n)<!-- end part2 -->/","",$old_html);
0
 
LVL 1

Author Comment

by:ScottNL1
ID: 35108984
Still no luck. It doesn't work  at all now.

I will comment further on how i am obtaining the string.

<?php
$form = @file_get_contents($page);

$form =  preg_replace("/<!-- begin something -->(.*)<!-- end something -->/","",$form);

echo $form;

die;
?>

the page

This works
<!-- begin something -->something<!-- end something -->

This doesn't
<!-- begin something -->
something
<!-- end something -->

0
 
LVL 82

Accepted Solution

by:
hielo earned 2000 total points
ID: 35109078
http://www.php.net/manual/en/reference.pcre.pattern.modifiers.php

$form =  preg_replace("/<!-- begin something -->(.*)<!-- end something -->/s","",$form);
0
 
LVL 1

Author Comment

by:ScottNL1
ID: 35109142
Knew it was something simple.

Thanks.
0

Featured Post

[Webinar] Cloud Security

In this webinar you will learn:

-Why existing firewall and DMZ architectures are not suited for securing cloud applications
-How to make your enterprise “Cloud Ready”, and fix your aging DMZ architecture
-How to transform your enterprise and become a Cloud Enabler

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

885 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question