Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #16557

Re: Any recommendations on using PHP to parse marked-up text?

From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Newsgroups comp.lang.php
Subject Re: Any recommendations on using PHP to parse marked-up text?
Date 2016-02-21 11:33 +0100
Organization PointedEars Software (PES)
Message-ID <1615686.hQ19P4Vg7h@PointedEars.de> (permalink)
References (7 earlier) <874md46h6a.fsf@bsb.me.uk> <na74nd$4v6$1@solani.org> <87wpq04x3b.fsf@bsb.me.uk> <1504490.9PMmeY6QeD@PointedEars.de> <87mvqu2655.fsf@bsb.me.uk>

Show all headers | View raw


Ben Bacarisse wrote:

> Thomas 'PointedEars' Lahn <PointedEars@web.de> writes:
>> Ben Bacarisse wrote:
>>> "Christoph M. Becker" <cmbecker69@arcor.de> writes:
>>>> Anyhow, what Thomas most likely wanted to convey is that
>>>> preg_replace_callback() is not general enough to process all markup
>>>> languages.
>>> Seems unlikely.
>> How did you get that idea?  Christoph has understood me correctly.
> 
> Hmm... I wonder why you just cut, then, without comment my explanation
> of why that's wrong.  Do you have nothing to say about that part?

The only thing that I can see that you said definitively in this thread 
about using preg_replace_callback() in the way you suggested is this:

>>>>> It's general enough to be handle nested constructs because Perl's REs
>>>>> are, in practice, much more powerful than REs should be.  It's good to
>>>>> about nested construct (which I'd do by repeated scanning from the
>>>>> inside out) but it's there for those cases where nothing else will do.

And that is wrong.  First of all, you are proceeding from a false 
assumption: preg_*() do _not_ support Perl’s RE; they support Perl-
*Compatible* RE which are not as powerful as Perl’s RE:

<http://php.net/manual/en/reference.pcre.pattern.differences.php>

Second, I have explicitly commented on your assertion that because recursion 
is possible with PCRE, they would be “general enough” for parsing markup 
languages.  I have given you a lot of good reasons, both theoretical and 
practical, why that is wrong (you derided them).  It is also wrong insofar 
as “general enough” is just weasel words:

,-<http://php.net/manual/en/regexp.reference.recursive.php>
| 
| […] Perl 5.6 has provided an experimental facility that allows regular 
                               ^^^^^^^^^^^^^^^^^^^^^
| expressions to recurse (among other things). The special item (?R) is 
| provided for the specific case of recursion. This PCRE pattern solves the 
| parentheses problem (assume the PCRE_EXTENDED option is set so that white 
| space is ignored): \( ( (?>[^()]+) | (?R) )* \)
| 
| […]
| The values set for any capturing subpatterns are those from the outermost 
  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| level of the recursion at which the subpattern value is set. If the 
  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| pattern above is matched against (ab(cd)ef) the value for the capturing 
| parentheses is "ef", which is the last value taken on at the top level. If 
| additional parentheses are added, giving \( ( ( (?>[^()]+) | (?R) )* ) \) 
| then the string they capture is "ab(cd)ef", the contents of the top level 
| parentheses. If there are more than 15 capturing parentheses in a pattern, 
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| PCRE has to obtain extra memory to store data during a recursion, which it 
  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| does by using pcre_malloc, freeing it via pcre_free afterwards. If no
                                                                  ^^^^^ 
| memory can be obtained, it saves data for the first 15 capturing 
  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| parentheses only, as there is no way to give an out-of-memory error from 
  ^^^^^^^^^^^^^^^^
| within a recursion.
| 
| […]
| The maximum length of a subject string is the largest positive number that 
| an integer variable can hold. However, PCRE uses recursion to handle 
                                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| subpatterns and indefinite repetition. This means that the available stack 
  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| space may limit the size of a subject string that can be processed by 
  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| certain patterns.
  ^^^^^^^^^^^^^^^^^

Third, cases “where nothing else will do”, as you implied, do not exist.  
Markup parsers exist, and can be written with PHP.  Most notably, PHP 
includes at least four ways to parse HTML and XML with libxml:

DOMDocument
<http://php.net/manual/en/domdocument.load.php>
<http://php.net/manual/en/domdocument.loadhtml.php>
<http://php.net/manual/en/domdocument.loadhtmlfile.php>
<http://php.net/manual/en/domdocument.loadxml.php>

SimpleXML
<http://php.net/manual/en/book.simplexml.php>

XML Parser
<http://php.net/manual/en/book.xml.php>

XMLReader
<http://php.net/manual/en/book.xmlreader.php>

See also: <http://php.net/manual/en/book.libxml.php>


The rest of your posting appears to be just full quote, /ad hominem/, 
misconceptions, and blissful ignorance, so I do not find it worth my 
precious time to be commented on.  (Just in case you are wondering.)

-- 
PointedEars
Zend Certified PHP Engineer 
<http://www.zend.com/en/yellow-pages/ZEND024953> | Twitter: @PointedEars2
Please do not cc me. / Bitte keine Kopien per E-Mail.

Back to comp.lang.php | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Any recommendations on using PHP to parse marked-up text? James Harris <james.harris.1@gmail.com> - 2016-02-18 12:47 +0000
  Re: Any recommendations on using PHP to parse marked-up text? Arno Welzel <usenet@arnowelzel.de> - 2016-02-18 15:52 +0100
    Re: Any recommendations on using PHP to parse marked-up text? James Harris <james.harris.1@gmail.com> - 2016-02-20 18:28 +0000
  Re: Any recommendations on using PHP to parse marked-up text? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-02-18 14:56 +0000
    Re: Any recommendations on using PHP to parse marked-up text? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-02-18 22:14 +0100
      Re: Any recommendations on using PHP to parse marked-up text? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-02-18 21:45 +0000
        Re: Any recommendations on using PHP to parse marked-up text? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-02-18 23:09 +0100
          Re: Any recommendations on using PHP to parse marked-up text? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-02-19 01:15 +0000
            Re: Any recommendations on using PHP to parse marked-up text? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-02-19 02:45 +0100
              Re: Any recommendations on using PHP to parse marked-up text? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-02-19 12:03 +0000
                Re: Any recommendations on using PHP to parse marked-up text? Tim Streater <timstreater@greenbee.net> - 2016-02-19 12:17 +0000
                Re: Any recommendations on using PHP to parse marked-up text? "Christoph M. Becker" <cmbecker69@arcor.de> - 2016-02-19 14:18 +0100
                Re: Any recommendations on using PHP to parse marked-up text? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-02-19 14:02 +0000
                Re: Any recommendations on using PHP to parse marked-up text? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-02-20 20:31 +0100
                Re: Any recommendations on using PHP to parse marked-up text? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-02-21 01:39 +0000
                Re: Any recommendations on using PHP to parse marked-up text? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-02-21 11:33 +0100
                Re: Any recommendations on using PHP to parse marked-up text? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-02-21 12:53 +0000
    Re: Any recommendations on using PHP to parse marked-up text? James Harris <james.harris.1@gmail.com> - 2016-02-20 18:29 +0000
      Re: Any recommendations on using PHP to parse marked-up text? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-02-20 20:32 +0100
  Re: Any recommendations on using PHP to parse marked-up text? Jerry Stuckle <jstucklex@attglobal.net> - 2016-02-18 10:43 -0500
    Re: Any recommendations on using PHP to parse marked-up text? James Harris <james.harris.1@gmail.com> - 2016-02-20 18:51 +0000
      Re: Any recommendations on using PHP to parse marked-up text? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-02-20 20:08 +0000
        Re: Any recommendations on using PHP to parse marked-up text? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-02-20 21:22 +0100
          Re: Any recommendations on using PHP to parse marked-up text? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-02-20 23:40 +0000
            Re: Any recommendations on using PHP to parse marked-up text? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-02-21 11:39 +0100
              Re: Any recommendations on using PHP to parse marked-up text? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-02-21 13:09 +0000
        Re: Any recommendations on using PHP to parse marked-up text? James Harris <james.harris.1@gmail.com> - 2016-02-20 20:24 +0000
          Re: Any recommendations on using PHP to parse marked-up text? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-02-20 22:35 +0000
            Re: Any recommendations on using PHP to parse marked-up text? James Harris <james.harris.1@gmail.com> - 2016-02-21 08:37 +0000
              Re: Any recommendations on using PHP to parse marked-up text? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-02-21 10:55 +0000
                Re: Any recommendations on using PHP to parse marked-up text? James Harris <james.harris.1@gmail.com> - 2016-02-21 13:25 +0000
          Re: Any recommendations on using PHP to parse marked-up text? Jerry Stuckle <jstucklex@attglobal.net> - 2016-02-20 17:50 -0500
            Re: Any recommendations on using PHP to parse marked-up text? James Harris <james.harris.1@gmail.com> - 2016-02-21 08:49 +0000
              Re: Any recommendations on using PHP to parse marked-up text? Jerry Stuckle <jstucklex@attglobal.net> - 2016-02-21 10:05 -0500
      Re: Any recommendations on using PHP to parse marked-up text? Jerry Stuckle <jstucklex@attglobal.net> - 2016-02-20 17:45 -0500
        Re: Any recommendations on using PHP to parse marked-up text? James Harris <james.harris.1@gmail.com> - 2016-02-21 09:01 +0000
          Re: Any recommendations on using PHP to parse marked-up text? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-02-21 11:46 +0100
            Re: Any recommendations on using PHP to parse marked-up text? James Harris <james.harris.1@gmail.com> - 2016-02-21 12:36 +0000
              Re: Any recommendations on using PHP to parse marked-up text? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-02-21 14:39 +0100
                Re: Any recommendations on using PHP to parse marked-up text? James Harris <james.harris.1@gmail.com> - 2016-02-21 13:40 +0000
                Re: Any recommendations on using PHP to parse marked-up text? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-02-21 14:46 +0100
                Re: Any recommendations on using PHP to parse marked-up text? James Harris <james.harris.1@gmail.com> - 2016-02-21 14:57 +0000
          Re: Any recommendations on using PHP to parse marked-up text? Jerry Stuckle <jstucklex@attglobal.net> - 2016-02-21 10:10 -0500
            Re: Any recommendations on using PHP to parse marked-up text? James Harris <james.harris.1@gmail.com> - 2016-02-21 17:50 +0000
              Re: Any recommendations on using PHP to parse marked-up text? Jerry Stuckle <jstucklex@attglobal.net> - 2016-02-21 14:08 -0500
      Re: Any recommendations on using PHP to parse marked-up text? Arno Welzel <usenet@arnowelzel.de> - 2016-02-21 18:35 +0100
        Re: Any recommendations on using PHP to parse marked-up text? James Harris <james.harris.1@gmail.com> - 2016-02-21 17:55 +0000
          Re: Any recommendations on using PHP to parse marked-up text? Arno Welzel <usenet@arnowelzel.de> - 2016-02-22 16:38 +0100
  Re: Any recommendations on using PHP to parse marked-up text? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-02-18 22:06 +0100
  Re: Any recommendations on using PHP to parse marked-up text? Ian Collins <ian-news@hotmail.com> - 2016-02-27 15:58 +1300

csiph-web