Solved

preg_replace

Posted on 2011-03-11
8
293 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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Easy Project Management (No User Manual Required)

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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 …

757 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now