Using php preg_match to get an id from forum quotes -


i've been using preg_match try , id quote in forums have written. have far.

$quote = '[quote]19[\quote] reply quote.';  $get = '/([quote])([0-9])([\quote])/';  $id = '';  preg_match($get, $quote, $id);  echo $id[0]; 

unfortunately doesn't give me result hoping , have tried many variations , tried preg_replace in hopes might give me need after lot of reading on stack overflow think preg_match way go. can't seem want id in between quote tags.

my experience preg limited @ best , i've tried best try work unfortunately beyond current knowledge assistance appreciated.

hints

  • [ , ] used character class delimiters.
    they must escaped \[, \] when taken literally.
    the definition [0-9] means this: character class of digits.
  • (…) brackets embrace result group.
    if wish extract numeric data between [quote] , [\quote] ([0-9]*?) should in brackets. result in $id[1] (group # 1).
  • the backslash "\" character in [\quote] must escaped too, escape character itself: [\\\\quote] (4 times \, since interpreted twice; somehow tricky, know).
    btw: maybe [/quote] meant; make easier (?)

code

<?php     $quote1 = '[quote]19[\quote] reply quote.';     $quote2 = '[quote]19[/quote] reply quote.';     // $get = '/\[quote\]([0-9].*?)\[\quote\]/';     $get1 = '%\[quote\]([0-9]*?)\[\\\\quote\]%';     $get2 = '%\[quote\]([0-9]*?)\[/quote\]%';     $id = '';     preg_match($get1, $quote1, $id);     echo '$get1,$quote1 :: ' . $id[1] . '<br />';     preg_match($get2, $quote2, $id);     echo '$get2,$quote2 :: ' . $id[1] . '<br />'; ?> 

output:
$get1,$quote1 :: 19
$get2,$quote2 :: 19

regex commented

    \[          # match character “[” literally     quote       # match characters “quote” literally     \]          # match character “]” literally     (           # match regular expression below , capture match backreference number 1        [0-9]       # match single character in range between “0” , “9”           *?          # between 0 , unlimited times, few times possible, expanding needed (lazy)     )     \[          # match character “[” literally         \\\\       # match character “\” literally     quote       # match characters “quote” literally     \]          # match character “]” literally 

Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -