regex - PHP Backtrack_Limit_Error on preg_replace when matching nothing -


converting possibly nested html ul csv (4 fields) using php preg_replace run onto snag. following line takes care of part of nested lists go unchanged (except removed newlines) 1 of fields created topmost ul:

$idx_string = preg_replace("|(<li>.*?)\n+(<ul>)\n+(.*?</li></ul></li>)|si","$1$2$3", $idx_string); 

now on large lists without nested lists (checked there's no such thing <ul> in @ point of conversion) fails due backtrack_limit_error. while know how on it, can't figure how matching nothing trigger backtrack limit @ all. according i've found, preg_replace returns either new string or unchanged old string (besides null/false on error). how backtrack in here?

the list items this:

<li><a href="9848.php">algeria - italy.</a></li>  <li>go sailing<br> <a href="11434.php">anglesey / wight / guernsey / jersey</a></li>  <li><a href="11367.php">d'anjou et du saumurois, carte des gouvernements</a><br> check out old places!</li> 

the csv looks this:

|9848.php|algeria - italy.| go sailing|11434.php|anglesey - anglesey / wight / guernsey / jersey| |11367.php|d'anjou et du saumurois, carte des gouvernements|check out old places! 

so in effect tags stripped , remainder split 4 fields. odd nested list stuffed third field is, <ul> & <li> tags, newlines stripped.

this old php 4 code utilized fallback mechanism. domdocument might better general approach, don't want invest time in , format of list pretty strict & simple.

summing up

looking @ code again jerry's comments in mind becomes obvious how first group (<li>.*?) has php starting @ first <li> right @ top of file , chewing whole file in search <ul>, 1 backtrack space.

enclosing statement in if (stripos($idx_string, '<ul')) { ... } block reduces chance of triggering error, raising pcre.backtrack_limit 1000000, default of php 5.3.7 anyway, not updated here 1 reason or another. wrapping record.


Comments

Popular posts from this blog

c# - How Configure Devart dotConnect for SQLite Code First? -

c++ - Clear the memory after returning a vector in a function -

erlang - Saving a digraph to mnesia is hindered because of its side-effects -