Jump to content


Photo

pattern matching


  • Please log in to reply
4 replies to this topic

#1 lxndr

lxndr
  • Members
  • PipPipPip
  • Advanced Member
  • 72 posts

Posted 02 June 2006 - 02:18 PM

Can anyone tell me how I could pattern match the following from a string and extract the matched text as a new string?

begins with '( '
then has a number of alphanumeric characters
ends with 'sion)'

Thanks in advance.




#2 poirot

poirot
  • Members
  • PipPipPip
  • Advanced Member
  • 646 posts
  • LocationAustin, TX

Posted 02 June 2006 - 03:18 PM

<?php

$str = '( confusion)';

preg_match('/\( ([A-Za-z0-9]*)sion\)/', $str, $m);
$match = $m[1];

echo $match;

?>

Will echo "confu"
~ D Kuang

#3 lxndr

lxndr
  • Members
  • PipPipPip
  • Advanced Member
  • 72 posts

Posted 02 June 2006 - 03:58 PM

[!--quoteo(post=379364:date=Jun 2 2006, 04:18 PM:name=poirot)--][div class=\'quotetop\']QUOTE(poirot @ Jun 2 2006, 04:18 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
<?php

$str = '( confusion)';

preg_match('/\( ([A-Za-z0-9]*)sion\)/', $str, $m);
$match = $m[1];

echo $match;

?>

Will echo "confu"
[/quote]

Hi,

Thanks for your message. I didn't quite explain myself correctly... what I want to be able to do is extract everything within the brackets .. e.g:

Blah blah blah (1943 version) ->> (1943 version)

Some more blah (original version) ->> (original version)




#4 poirot

poirot
  • Members
  • PipPipPip
  • Advanced Member
  • 646 posts
  • LocationAustin, TX

Posted 02 June 2006 - 04:12 PM

Oh, OK:

<?php

$str = 'Blah blah blah (1943 version)';

preg_match('/\(([A-Za-z0-9]*) version\)/', $str, $m);
$match = $m[0];

echo $match;

?>

~ D Kuang

#5 lxndr

lxndr
  • Members
  • PipPipPip
  • Advanced Member
  • 72 posts

Posted 02 June 2006 - 04:20 PM

[!--quoteo(post=379383:date=Jun 2 2006, 05:12 PM:name=poirot)--][div class=\'quotetop\']QUOTE(poirot @ Jun 2 2006, 05:12 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Oh, OK:

<?php

$str = 'Blah blah blah (1943 version)';

preg_match('/\(([A-Za-z0-9]*) version\)/', $str, $m);
$match = $m[0];

echo $match;

?>
[/quote]

Thanks for your help. Much appreciated !






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users