Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Try changing your regular expression into</p> <pre><code>/foo\s*{\s*bar:\s*([A-Za-z]*)\s*}/ </code></pre> <p>and then look at the output again. You will then probably see entries in your output with only the text you want to fetch.</p> <p>By using <code>(</code> and <code>)</code> you create a group within your regular expression, and the preg_match_all function will output the content only within those groups as well.</p> <h2>Output array</h2> <p>An example:</p> <pre><code>$text = 'Here comes a number: 5, here comes a number: 3 and here comes a number: 4'; preg_match_all( '/[Hh]ere comes a number: ([0-9])/', $text, $matches ); </code></pre> <p>After running this code, <code>$matches</code> will now be:</p> <pre><code>array( array( 'Here comes a number: 5', '5' ), array( 'Here comes a number: 5', '5' ), array( 'Here comes a number: 5', '5' ) ) </code></pre> <p>As you can see, <code>$matches</code> will contain an array for every string that matches. The first entry (<code>$matches[0]</code>) will always contain the complete matched string. The other indices (<code>$matches[1]</code>, <code>$matches[2]</code> and so on) will only contain the value of the specified groups in order. If you specify an optional group (for example <code>test([0-9])?</code>) the associated index will contain a <code>null</code> value.</p> <h2>Excluding groups from the output</h2> <p>Sometimes you want to specify a group but don't want to include it in the output array. For example:</p> <pre><code>$text = 'Here comes a number: 5, here comes another number: 3 and here comes a number: 4'; preg_match_all( '/[Hh]ere comes a(nother)? number: ([0-9])/', $text, $matches ); </code></pre> <p>I added a group for <code>nother</code> because I want it to be optional. Now my <code>$matches[1]</code> contains <code>"nother"</code> or <code>null</code>, and my <code>$matches[2]</code> contains the actual number. Since I'm not interested in whether the user chose to write <em>"another"</em> or <em>"a"</em>, I would like to exclude this group from the output.</p> <p>This can be done by starting the group with <code>(?:</code>. The resulting code:</p> <pre><code>$text = 'Here comes a number: 5, here comes a number: 3 and here comes a number: 4'; preg_match_all( '/[Hh]ere comes a(?:nother)? number: ([0-9])/', $text, $matches ); </code></pre> <p>The group <code>(?:nother)</code> gets ignored in the output, and <code>$matches[1]</code> references to the actual number we are interested in.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload