?
Solved

preg_replace

Posted on 2011-03-11
8
Medium Priority
?
303 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

Technology Partners: 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!

Question has a verified solution.

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

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.
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

719 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