views:

44

answers:

1

Hi, my preg_replace replaces my whole string instead of just the bit where the expression fits.

Code:

http://beta.yapaste.com/bd

This is what I want replaced:

<table id=\"post24100391\" style=\"width: 100%;\" class=\"p4\" >

Thanks for help.

+1  A: 

yes..... that regex matches the entire table.... it will replace the entire string with $replace.

what is it you want to replace?

You could use capturing replacement...

preg_replace("/(<table.*?>).*(<\/table>)/","\$1$replace\$2},$str);

Or you could use a non-capturing group around the parts to not replace...

e.g.

preg_replace("/(?:<table.*?>).*(?:<\/table>)/",$replace,$str) //not tested, though

EDIT in response to OP change

preg_replace("/<table.*?>/",$replace,$str);

You wanted to use lazy capturing *?

Jonathan Fingland