Forum Moderators: coopster

Message Too Old, No Replies

using variables in preg replace

         

taiwanho

8:21 am on Mar 27, 2007 (gmt 0)

10+ Year Member



I need to replace a chunk of html code in 100 odd pages for a project. So I have written a php script that opens the files, does a preg_replace to cut the code I don't want then save the file (and loop over the rest).

However, I am having trouble with the preg_replace.

This works:

$new_contents = preg_replace ("/$replace1(.*)<\/td>/", "cut out", $contents);

Where $replace1 is the beginning part of the html I want to cut.

But, I want to specify the second part and not just use <\/td>. So I thought it would be:

$new_contents = preg_replace ("/$replace1(.*)$replace2/", "cut out", $contents);

But this doesn't work. Could someone check my syntax, please? Or is it even possible to have two variables in the expression?

Many thanks in advance.

mcibor

8:41 am on Mar 27, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I think you mix something:

[php.net...]
first part is a pattern, then replacement, and last is a string in which the replacement takes part.

In the pattern you can put as many variables as you like, just remember it needs to be still a valid pattern.
What I would do is move the pattern outside of the preg:

$pattern = "/$replace1(.*)$replace2/";
$new_contents = preg_replace ($pattern, "cut out", $contents);

this way you can echo $pattern; and check if it is valid.

Hope this helps
Michal

taiwanho

6:36 am on Mar 30, 2007 (gmt 0)

10+ Year Member



Hi Michal,

Many thanks for your reply. I guess the problem is with my understanding of the pattern.

Basically I want to replace a large chunk of html from a file like this:


<html>
<head>
<title> Cutting </title>
</head>

<body>

from

<p>This is the chunk I want to remove</p>

here

</body>

</html>

So replace everything from "from" to "here" by doing this:

$replace1 = "from";
$replace2 = "here";
$pattern = "/$replace1(.*)$replace2/";
$new_contents = preg_replace ($pattern, "cut out", $contents);

where $contents is the html file. Am I being too hopeful?

eelixduppy

10:55 am on Mar 30, 2007 (gmt 0)



Try taking the variables out of the string:

$pattern = "/(".$replace1.".+".$replace2.")/i";

mcibor

1:42 pm on Mar 30, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I think the problem is a bit different.
I think, that $replace2 contains some special characters, which should for regex be escaped.

Could you provide us with real example of the ending?

Michal

PS. Eelix's solution could also work, if you don't use separate $pattern, then I am not sure how will $ be parsed.

eelixduppy

1:48 pm on Mar 30, 2007 (gmt 0)



In order to escape special characters you would only need to use preg_quote [php.net]. Something like this:

$pattern = "/(".preg_quote($replace1,'/').".+".preg_quote($replace2,'/').")/i";

taiwanho

9:37 am on Apr 2, 2007 (gmt 0)

10+ Year Member



Thanks for the replies. I tried those suggestions, but it is still not working.

It must be in my pattern. When I try this:

$contents = "
from
<p>This is the chunk I want to remove</p>
here
";

it doesn't work, but this works fine:

$contents = "
from<p>This is the chunk I want to remove</p>here
";

So this is the whole script:

<?php

$replace1 = "from";
$replace2 = "here";

$contents = "
from
<p>This is the chunk I want to remove</p>
here
";

// $pattern = "/(".preg_quote($replace1,'/').".+".preg_quote($replace2,'/').")/i";
// $pattern = "/(".$replace1.".+".$replace2.")/i";
$pattern = "/$replace1(.*)$replace2/";

echo "The pattern is ".$pattern."<br><br>";
$new_contents = preg_replace ($pattern, "cut out", $contents);

echo $new_contents;

?>

Thanks again for any insight.

joelgreen

1:29 pm on Apr 2, 2007 (gmt 0)

10+ Year Member



try changing pattern to $pattern = "/$replace1(.*)$replace2/i";

i at the end means "multiline"

eelixduppy

7:22 pm on Apr 2, 2007 (gmt 0)



>>i at the end means "multiline"

The 'i' actually means to ignore the case. 'm' is the modifier for 'multiline' patterns, however, I don't think it applies here.

[us.php.net...]

You may want to try a pattern like this:


$pattern = "/(".$replace1."[\r\n.]+".$replace2.")/";

taiwanho

6:24 am on Apr 4, 2007 (gmt 0)

10+ Year Member



I went through that link and found the answer. It was actually /s at the end.

Now it works a treat!

Thanks again for the help.

eelixduppy

10:28 am on Apr 4, 2007 (gmt 0)



hehe, it seems as though I missed that modifier ;) Glad you got it, taiwanho!

joelgreen

11:55 am on Apr 4, 2007 (gmt 0)

10+ Year Member



Whoops, sorry. Deleted wrong character from my pattern. I usually add /is at the end of the pattern. Glad you managed to resolve it :)