Jump to content

Recommended Posts

I need to extract some information from a text string. The information is between two curly braces { my stuff i need here }

 

here is my regex call

 

 

preg_match('/\{([^}]+)\}/',$pdfString,$info)

 

This works and $info is filled with content, but it is not correct this is what I get from it.

 

 

 

 
//this is part of the string. It is being read in from a text file generated by another system.
 
$string = '2,563.71


{companyName



}



{companyContact }
';
 
//when I do a print_r of the output from the preg_match_all command the following is what I get.
 
Array

(

      [0] => Array

      (

                   [0] => {companyName



       }

                   [1] => {companyContact }

       )



       [1] => Array

       (

                    [0] => companyName





                    [1] => companyContact

        )



)
 

 

What is wrong with my preg_match_all command that I am getting 2 items in my array that are basically filled with the same thing only the array[1] has no curly braces, and array[0] does. How do I get just information between the curly braces?

 

Thanks in advance,

Chris 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/275885-preg_match_all-sorta-working/
Share on other sites

 int preg_match ( string $pattern , string $subject [, array &$matches [, int $flags = 0 [, int $offset = 0 ]]] )

 

If matches is provided, then it is filled with the results of search. $matches[0] will contain the text that matched the full pattern,$matches[1] will have the text that matched the first captured parenthesized subpattern, and so on.

It helps to read the manual.

http://php.net/manual/en/function.preg-match.php

i did read the manual, but what that says to me is that if this is the string 

 

'{companyName}{companyContact}'

 

then the result would be

 

$match[0] = companyName

$match[1] = companyContact

 

and that is not what I am getting.

 

I am getting an array that looks like this

 

$match[0][0] = '{companyName}'

$match[0][1] = '{companyContact}'

$match[1][0] = 'companyName'

$match[1][1] = 'companyContact'

 

this is causing a problem because some the matches are coming back like the following

 

$match[0][0] = '{companyName {companyContact}'

$match[0][1] = '{companyContact}'

$match[1][0] = 'companyName {companyContact'

$match[1][1] = 'companyContact

 

so there seems to be something wrong with my regex expression .. which is what my question was about.

 

Chris

Edited by severndigital

ook closer,.. that is what it is supposed to do.

 

$matches[0] will contain the text that matched the full pattern,

$matches[1] will have the text that matched the first captured parenthesized subpattern, and so on.

 

Also, in your string example, you have the curly braces spanned out on several lines with whitespace in between the subject and the curly brace boundaries.

 

 

string = '2,563.71


{companyName



}



{companyContact }
';

 

If you are trying to extend beyond those lines with Regex, you will need to use a Pattern Modifier

 

m (PCRE_MULTILINE) By default, PCRE treats the subject string as consisting of a single "line" of characters (even if it actually contains several newlines). The "start of line" metacharacter (^) matches only at the start of the string, while the "end of line" metacharacter ($) matches only at the end of the string, or before a terminating newline (unless D modifier is set). This is the same as Perl. When this modifier is set, the "start of line" and "end of line" constructs match immediately following or immediately before any newline in the subject string, respectively, as well as at the very start and end. This is equivalent to Perl's /m modifier. If there are no "\n" characters in a subject string, or no occurrences of ^ or $ in a pattern, setting this modifier has no effect.

So, your pattern basically just needs an m at the end is all I'm getting at.

 

 

preg_match('/\{([^}]+)\}/m',$pdfString,$info)

 

 

Also there are several other dilimeters other than / like @#$%

 

 

preg_match('#\{([^}]+)\}#m',$pdfString,$info)

 

 

That way you can clearly identify your pattern and any modifiers outside of it

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.