MCrosbie Posted January 21, 2007 Share Posted January 21, 2007 Hi there,I am trying to use Preg_match to do the following:[code]$content = 'ajklsfnkljsanfjkndjsnfadsnfdnjnk {AO_photogallery_cat #100192 } asjkdfnkjadsna'; // This is actually pulled off a database$variable = '{AO_photogallery_cat #[0-9]{6} }';if(preg_match($variable,$content,$matches){ $var['varivari'] = explode("#",$matches[0]); // Here I am trying to get the number by itself and set it as a variable $var['varivari'] = substr($var['varivari'][1], -1); echo $var['varivari'];} [/code]Please help me get this preg_match thing to work. I dont understand PCRE Pattern Syntax so if someone could explain the basic concepts that would be fantastic. Quote Link to comment Share on other sites More sharing options...
linuxdream Posted January 21, 2007 Share Posted January 21, 2007 Your $variable sould be something like:$variable = '/AO_photogallery_cat #([0-9])\b/';then $matches[1] would contain the number. Quote Link to comment Share on other sites More sharing options...
MCrosbie Posted January 21, 2007 Author Share Posted January 21, 2007 Hey thanks for replying!So first what do all the slashes etc mean? I tried reading the PCRE Pattern Syntax but it didn't make much sense. And what should it be in the content? Quote Link to comment Share on other sites More sharing options...
linuxdream Posted January 21, 2007 Share Posted January 21, 2007 The content can be whatever you want it to be. No regex stuff needed. The first and last "/" are delimiters that tell preg_match that the content within the /'s is to used in the expression. The ( )'s capture the content within them and they get placed in the $matches array, hence $matches[1]. So in this case it captures the numbers only, that way you don't have to substr() or whatever after to get just what you want. The \b says that it should end with a word boundary. Could have just been a \s or space but they both should work in this case. Quote Link to comment Share on other sites More sharing options...
MCrosbie Posted January 21, 2007 Author Share Posted January 21, 2007 Hi again,I tried this[code] $content = 'ajklsfnkljsanfjkndjsnfadsnfdnjnk {AO_photogallery_cat #100192 } asjkdfnkjadsna'; // This is actually pulled off a database$variable = '/AO_photogallery_cat #([0-9])\b/';if(preg_match($variable,$content,$matches)){ echo $matches[1];} [/code]But it still doesn't work. Quote Link to comment Share on other sites More sharing options...
MCrosbie Posted January 21, 2007 Author Share Posted January 21, 2007 TO MODERATOR: Please move this post into the forum Regex within PHPCheers! Quote Link to comment Share on other sites More sharing options...
linuxdream Posted January 22, 2007 Share Posted January 22, 2007 Sorry, I forgot to include the *...so:$variable = '/AO_photogallery_cat #([0-9]*)\b/is'; Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.