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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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