views:

170

answers:

3

I have content that is first htmlentities and then stripslashes followed by nl2br.

This means a watermark at the end ends up as:

<li><p><!-- watermark --></p></li>

Not very useful. I have the code below to try and strip the html comments and stop it displaying but its not very good at it!

$methodfinal = str_replace('<li><p><!--', '<!--', $method);
$methodfinal2 = str_replace('--></p></li>', '-->', $methodfinal);
echo $methodfinal2;

anyone got any ideas?

A: 

Something like this?

$final = preg_replace("/<li><p>(<!--.*-->)<\/p><\/li>/", "$1", $original);
Zed
A: 

EDIT: following Zed's and your comments I've done some testing and this is what you should use:

$final = preg_replace('/<li><p>[\s]*?&lt\;!--(.*?)--&gt\;<\/p><\/li>/m', "<!--$1-->", $z);

Here is a breakdown of the RE:

<li><p>

this is obvious

[\s]*?

because you have a few spaces and a newline between the <li> and the comment, but we want the least number of newlines so we use the non greedy *? (it sould work with * as well)

&lt\;

need to escape the ;

!--(.*?)--

again we use *? so we would match only this line (other wise if you had the same line again it wold match from the first one to the last one

&gt\;<\/p><\/li>

same as above

/m'

so php would treat newlines as whitespace (i am not sure about this but it seems to be working)

Nir Levy
no luck... http://www.whatcouldicook.com/recipes/1177
bluedaniel
So now we can only hope for not having a > sign in the comment ;)
Zed
Daniel, it's not working, because your html source is incorrect. The comment have <!-- and --> on the sides instead of < and >.
Zed
@Daniel, Zed is right please do not use what i wrote. @Zed you are right, i should have used non-greedy ?* but i don't have any way of testing this now. sorry.
Nir Levy
so $final = preg_replace("/<li><p>( wont work?
bluedaniel
anyone hazard a guess? it shouldnt be too difficult to preg_replace two tags?
bluedaniel
thank you so much!! What would i do without Stack?
bluedaniel
A: 

@Zed:

Lets be more caring:

$final = preg_replace("/<li><p>(<!--.*?-->)<\/p><\/li>/", "$1", $original);
# use .*? every time over .* unless you specificly want what it does
# .*? matches as less as it can
# .* matches as much as it can

even better:

$final = preg_replace("/<li><p>(<!--[^\-\>]+-->)<\/p><\/li>/", "$1", $original);
# [^\-\>]+ will look for any character that is not - or > 
# so will perform faster

Just trying to advocate better regex practice. Hope this helps.

vulcan_hacker
well the FINAL winning statment was:preg_replace('/<li><p>[\s]*?!--(.*?)--<\/p><\/li>/m', "<!--$1-->", $original);can it be improved in the same manner?
bluedaniel
There is no point using <b>[\s]*?</b>. You should use <b>\s*</b>. If you had a set of characters like say spaces or digits, you could use [\s\d] or [\s0-9]. To make it match more than single character, use + (1 or more) or * (zero or more) or ? (none or one) after it. Then think about using *? to match zero or more, (match as minimum as possible). You can understand \s*? is not as sensible as .*?So use \s+ (at least one space, may be more ) or \s* (zero or more space) or \s? (zero or one space) here.preg_replace('/<li><p>\s*!--(.*?)--<\/p><\/li>/m', "<!--$1-->", $original);
vulcan_hacker
Why can't I format my comment? Can the moderators do that?
vulcan_hacker