[thelist] php pattern matching

Bob Meetin bobm at dottedi.biz
Sun Feb 13 14:47:13 CST 2011


Matt Warden wrote:
> On Sun, Feb 13, 2011 at 12:16 PM, Anthony Baratta <anthony at baratta.com> wrote:
>   
>> On 2/13/2011 9:39 AM, Bob Meetin wrote:
>>     
>>> {12345|tom smith|whatever title} oh what a beautiful morning, yadda yadda
>>>       
>> RegEx
>>
>> ^{(.*)}(.*)$
>>
>> When you check the Matches object, you should have
>>
>> $1 :: 12345|tom smith|whatever title
>>
>> $2 :: oh what a beautiful morning, yadda yadda
>>
>> This assumes you don't have any returns/line feeds in your "text" area.
>>
>> e.g.
>>
>> <?php
>> $ptn = "/^{(.*)}(.*)$/";
>> $str = "{12345|tom smith|whatever title} oh what a beautiful morning, yadda
>> yadda ";
>> preg_match($ptn, $str, $matches);
>> print_r($matches);
>> ?>
>>
>> Array
>> (
>>    [0] => {12345|tom smith|whatever title} oh what a beautiful morning,
>> yadda yadda
>>    [1] => 12345|tom smith|whatever title
>>    [2] =>  oh what a beautiful morning, yadda yadda
>> )
>>     
>
> Anthony, small tweak. I think you might run into trouble if the
> descriptive text ever contains a } character. If you look back at the
> regex:
>
> $ptn = "/^{(.*)}(.*)$/";
>
> We're saying: find a { as the first char. Then find any number of any
> char. This (.*) sequence would actually also match }. Then you're
> saying, match a } followed by any number of any char.
>
> I think the pattern you really want is:
>
> $ptn = "/^{([^}]+)}(.*)$/";
>
> This is saying: find a { as the first char. Then find one or more of
> any char except }. Then match a } followed by any number of any char
Thanks to both of you, but for this situation it would not work because 
the description part is expected to have many lines, carriage returns.  
I tested and it works fine assuming a single line.  I kept searching 
(googled php get content between) and found another option that seems to 
work.  90% of the problem is knowing what to google.

function extract_unit($string, $start, $end)
{
  $pos = stripos($string, $start);
  $str = substr($string, $pos);
  $str_two = substr($str, strlen($start));
  $second_pos = stripos($str_two, $end);
  $str_three = substr($str_two, 0, $second_pos);
  $unit = trim($str_three); // remove whitespaces
  return $unit;
}

$text = "{122345|Mister Happy Jack|Entwhistle}";
$text .="Happy Jack is a boy, dah dah dah dah dah!
Happy Jack is a boy, dah dah dah dah dah!
Happy Jack is a boy, dah dah dah dah dah!"; // just to add more lines 
for testing
echo "ALL TEXT: $text<br /><br />";

$unit = extract_unit($text, '{', '}');
echo "BRACKETED TEXT: $unit<br /><br />";

$text = eregi_replace ( "$unit", "", $text);
$text = eregi_replace ( "{", "", $text);
$text = eregi_replace ( "}", "", $text);
$text = eregi_replace ( "\|", "", $text);
echo "DESCRIPTION: $text";

-Bob


More information about the thelist mailing list