Friday, 15 March 2013

php - preg_replace returns empty string when using lot of spaces -



php - preg_replace returns empty string when using lot of spaces -

i know it not recommended parse xml / html reg-ex, trying simple thing:

<?php echo phpversion()."<br><br>"; $test_1 = '<tag attr="attr_value">tag_value</tag>'; $test_2 = $test_1.str_repeat(' ',1000); $test_3 = $test_1.str_repeat(' ',2000); $match = '!<(.*?) (.*?)="(.*?)">!'; $replace = '<\\2>\\3</\\2><\\1>'; $output_1 = preg_replace($match, $replace, $test_1); $output_2 = preg_replace($match, $replace, $test_2); $output_3 = preg_replace($match, $replace, $test_3); echo "xml: ".htmlspecialchars($test_1)."<br>"; echo "1: ".htmlspecialchars($output_1)."<br>"; echo "2: ".htmlspecialchars($output_2)."<br>"; echo "3: ".htmlspecialchars($output_3)."<br>"; ?>

i mean, putting attribute , value out of container tag. working fine test_1 , test_2 examples, if add together more spaces in test_3, homecoming string empty. can seek code?

in illustration works adding 1411 spaces. 1 more (1412) , doesn't ...

i have tested on 5.3.8 , 5.3.19 php versions.

thanks.

works fine me on php 4.4.8 command line. look seems inefficient. causes kind of error, e.g. out of memory, , preg_replace returns null means "error". here optimized version of expression:

<(\s*?) (\s*?)="([^"]*?)">

php regex preg-replace

No comments:

Post a Comment