Jump to content

workaround for variable length lookbehind in preg_match


TOgakangaroo

Recommended Posts

While writting this post I realized the problem  but I still don't have a solution - I need a lookbehind of variable width - all the text that follows a line that contains a certain format, how do I do I get around this limitation?  Here are the contents of my previous post with my example:

 

Hi guys, I'm going nuts with this problem.  Im simply trying to pull out the contents of a string following a certain string type.  Here's my sample program:

 

<?php
$str = "Date:             Thu 20-Mar-2008 13:41:37
Location:         PHILADELPHIA
User:             ABCDE

Please change densitites to 12.88 for tank 1353 for rec # 1234 and for all shipments from 3/19/08 to present.
I did change tank density.

Thanks 
";
$pattern = '/(?<=(User:\s*\w*\S)).*/sm';
if(preg_match($pattern, $str, $matches)) {printpre($matches); }
else { printpre("Matches Not Found"); }

function printpre($var) {print("<pre>");print_r($var);print("</pre>");} 
?>

 

To my understanding, $pattern should match all text preceeded by (User:<some spaces><some letters><some whitespace>) which should be the whole message part of the search string (following the 'User: ' line).  The results however: "Matches not found".

 

Oddly, the following works:

$pattern = '/(?<=(User:            \w\w\w\w\w\s)).*/sm';

 

<pre>
<?php
$str = "Date:             Thu 20-Mar-2008 13:41:37
Location:         PHILADELPHIA
User:             ABCDE

Please change densitites to 12.88 for tank 1353 for rec # 1234 and for all shipments from 3/19/08 to present.
I did change tank density.

Thanks 
";
list($metadata, $data) = preg_split('/User:\s*\S*\s+/', $str);
echo $data;
?>
</pre>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.