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';

 

Link to comment
Share on other sites

<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>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.