Jump to content

Preg_match delimiters


MCrosbie

Recommended Posts

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.
Link to comment
https://forums.phpfreaks.com/topic/35121-preg_match-delimiters/
Share on other sites

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.
Link to comment
https://forums.phpfreaks.com/topic/35121-preg_match-delimiters/#findComment-165820
Share on other sites

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.
Link to comment
https://forums.phpfreaks.com/topic/35121-preg_match-delimiters/#findComment-165823
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.