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
Solved

preg_replace

Posted on 2011-03-11
8
296 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

 
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
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 look for a specific file type in a local or remote server directory using PHP.

809 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