Reputation: 745
I really don't understand regex at all, and it hurts my head.
I've a bit of text which looks like this
blah blah blah (here is the bit I'd like to extract)
...and I don't really understand how to extract this using PHP's preg_split, or equivalent, command.
How do I do this? And where's a good place to understand how preg works?
Upvotes: 1
Views: 1663
Reputation: 944
<?php
$text = "blah blah blah (here is the bit I'd like to extract)";
$matches = array();
if(preg_match('!\(([^)]+)!', $text, $matches))
{
echo "Text in brackets is: " . $matches[1] . "\n";
}
Upvotes: 2
Reputation: 400972
Something like this should do the trick, to match what is between (
and )
:
$str = "blah blah blah (here is the bit I'd like to extract)";
if (preg_match('/\(([^\)]+)\)/', $str, $matches)) {
var_dump($matches[1]);
}
And you'd get :
string 'here is the bit I'd like to extract' (length=35)
Basically, the pattern I used searches for :
(
; but as ( has a special meaning, it has to be escaped : \(
[^\)]+
([^\)]+)
$matches[1]
)
; here, too, it's a special character that has to be escaped : \)
Upvotes: 4