1

I am running a RST to php conversion and am using preg_match.

this is the rst i am trying to identify:

An example of the **Horizon Mapping** dialog box is shown below. A 
summary of the main features is given below. 

.. figure:: horizon_mapping_dialog_horizons_tab.png 

   **Horizon Mapping** dialog box, *Horizons* tab 

Some of the input values to the **Horizon Mapping** job can be changed 
during a Workflow using the internal programming language, IPL. For 
details, refer to the *IPL User Guide*. 

and I am using this regex:

$match = preg_match("/.. figure:: (.*?)(\n{2}[ ]{3}.*\n)/s", $text, &$result);

however it is returning as false. here is a link of the expression working on regex http://regex101.com/r/oB3fW7.

tht13
  • 47
  • 3
  • 8
  • Have you tried `\r\n`? – Niet the Dark Absol Jun 17 '14 at 13:49
  • yes i have, i used \r?\n\r?\n – tht13 Jun 17 '14 at 13:56
  • Why the `s` flag? You do have a newline at the end of your input? How do you check the result? It seems like `\R` should work, can you add more info? – Robin Jun 17 '14 at 14:42
  • I used the s flag because I read on a similar problem that the s flag did something with a double line feed he $match variable is a boolean value and will return true if a match is found yes there is a new line after the text I have updated the question with more rst – tht13 Jun 18 '14 at 06:05

2 Answers2

2

Are you sure that the line break is \n, is doubt, use \R:

$match = preg_match("/.. figure:: (.*?)(\R{2}[ ]{3}.*\R)/s", $text, &$result);

\R stands for either \n, \r and \r\n

Toto
  • 89,455
  • 62
  • 89
  • 125
  • it still wont find it, if i remove a line and only search for one line feed it works however the second messes it up – tht13 Jun 17 '14 at 13:55
0

My instinct would be to do some troubleshooting around the s flag as well as the $result variable passed by reference. To achieve the same without any interference from dots and the return variable, can you please try this regex:

..[ ]figure::[ ]([^\r\n]*)(?:\n|\r\n){2}[ ]{3}[^\r\n]*\R

In code, please try exactly like this:

$regex = "~..[ ]figure::[ ]([^\r\n]*)(?:\n|\r\n){2}[ ]{3}[^\r\n]*\R~";
if(preg_match($regex,$text,$m)) echo "Success! </br>";

Finally:

If this does not working, you might have a weird Unicode line break that php is not catching. To debug, for each character of your string, iterate through all the string's characters

  1. Iterate: foreach(str_split($text) as $c) {
  2. Print the character: echo $c . " value = "
  3. Print the value from this function: . _uniord($c) . "<br />"; }
Community
  • 1
  • 1
zx81
  • 41,100
  • 9
  • 89
  • 105
  • The regex you provided didnt work, however I tried the uniord and it gave me a weird output that the line feeds were spaces, and the 3 spaces before the ** on the second line were not even there, is there a site where I could upload my files you you to see what I mean? $result is also returning an emtpy array – tht13 Jun 18 '14 at 06:25