php - preg_match() not working in some cases -
i feel should such easy 'change comma', i've done research , tried many different things, nothing seems work. first code used attempt debug it:
/* more code before */ $test = "this test <ul>test</ul> blabla"; $real = $data['chapters']['introduction']; var_dump($real); echo "\n\n"; preg_match('/<ul>(.*)<\/ul>/', $test, $vartest); var_dump($vartest); echo "\n\n"; preg_match('/<ul>(.*)<\/ul>/', $real, $varreal); var_dump($varreal);
the result this:
string(1888) "<p>the <b>theory of relativity</b>, or <b>relativity</b>, encompasses 2 theories of <a href="http://en.wikipedia.org/wiki/albert_einstein" title="albert einstein">albert einstein</a>: <a href="http://en.wikipedia.org/wiki/special_relativity" title="special relativity">special relativity</a> , <a href="http://en.wikipedia.org/wiki/general_relativity" title="general relativity">general relativity</a>. concepts introduced theories of relativity include:</p> <ul> <li> <p>measurements of various quantities <i>relative</i> velocities of observers. in particular, space , time can <a href="http://en.wikipedia.org/wiki/time_dilation" title="time dilation">dilate</a>.</p> </li> <li> <p><a href="http://en.wikipedia.org/wiki/spacetime" title="spacetime">spacetime</a>: space , time should considered , in relation each other.</p> </li> <li> <p>the speed of light nonetheless invariant, same observers.</p> </li> </ul> <p>the term "theory of relativity" based on expression "relative theory" (<a href="http://en.wikipedia.org/wiki/german_language" title="german language">german</a>: <span lang="de"><i>relativtheorie</i></span>) used <a href="http://en.wikipedia.org/wiki/max_planck" title="max planck">max planck</a> in 1906, emphasized how theory uses <a href="http://en.wikipedia.org/wiki/principle_of_relativity" title="principle of relativity">principle of relativity</a>. in discussion section of same paper <a href="http://en.wikipedia.org/wiki/alfred_bucherer" title="alfred bucherer">alfred bucherer</a> used first time expression "theory of relativity" (<a href="http://en.wikipedia.org/wiki/german_language" title="german language">german</a>: <span lang="de"><i>relativitätstheorie</i></span>).</p> " array(2) { [0]=> string(13) "<ul>test</ul>" [1]=> string(4) "test" } array(0) { }
any idea on why last array empty (when should contain 3 list elements)?
some more info, retrieved mysql using pdo, i've tried escaping (for quotes), replacing quotes, checked text size way below preg_match() string limit, cannot find problem is. think code speaks problem is, anyway, i'd gladly perform tests need. thanks.
the biggest problem have here trying parse html code using regex. if can work data have, data contains nested <ul>
tags, regex blow up, , @ point become extremely difficult working. parsing html ought done using dom parser (ie php's domdocument class). regex wrong tool job.
that said, if must regex, need use s
modifier, due input being across multiple lines. modifier changes behaviour of dot character in regex includes line feed characters.
so final pattern needs this:
preg_match('/<ul>(.*)<\/ul>/s', $real, $varreal);
hope helps.
Comments
Post a Comment