Solved

preg_replace

Posted on 2011-03-11
8
300 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: 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 500 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
This article discusses how to implement server side field validation and display customized error messages to the client.
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

734 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