Page 1 of 1

preg_replace/eregi_replace issue

Posted: Mon Jun 30, 2003 1:11 pm
by m3rajk
i'm not sure if the issue is in the function i made or in the eregi. i know the function i made returns every other one and the first one, if it starts with a [nocode][/nocode] block is an empty string. i built that up in a different file to make sure i had the function working right (only it wasn't a function there, so i might be returning wrong)

for those interested in playing with the code as it is and seeing the full code, here's the full code (minus the opening <?php): http://people.brandeis.edu/~m3rajk/JMT/ ... 2.disp.txt and the code in action: http://people.brandeis.edu/~m3rajk/JMT/ ... a-fcv2.php

the test string i have been using: [nocode]what will happen with this block?[/nocode] and then this should be bold [nocode][c]and this should not be centered[/c][/nocode] and i can't think of more

here's the function and the added replacement:

Code: Select all

function get_sub_pattern($pattern, $inputstring){
  $stage1=preg_split($pattern, $inputstring, -1, PREG_SPLIT_DELIM_CAPTURE);
  $stage2=array();
  $blocks=count($stage1);
  for($i=0;$i<$blocks;$i++){
    if(!(is_long($i/2))){
      $stage2[]=$stage1[$i];
    }
  }
  return $stage2;
}

# make replacements

# code exempt section (remove)
$codeexempt=get_sub_pattern('|\[nocode]([^]]*)\[/nocode]|i', $precode);
$postcode=eregi_replace('\[nocode][^]]*\[/nocode]', '&&&', $precode);

Posted: Tue Jul 01, 2003 12:20 am
by Tubbietoeter
i believe you need to quote the brackets [ or ] your looking for.

Posted: Tue Jul 01, 2003 3:15 am
by patrikG

Code: Select all

'\&#1111;nocode]&#1111;^]]*\&#1111;/nocode]'
change to

Code: Select all

'\&#1111;nocode]&#1111;^]]*&#1111;\/nocode]'

Posted: Tue Jul 01, 2003 4:40 am
by forgun
what that mean

Code: Select all

'\&#1111;nocode]&#1111;^]]*&#1111;\/nocode]'

Posted: Tue Jul 01, 2003 4:50 am
by patrikG
It means that to have "/nocode" as search-pattern, you will need to escape "/".
For more on this: tutorial 1, there is also a nice tutorial at phpbuilder, but I've lost the URL.
If in doubt, google. :)

Posted: Tue Jul 01, 2003 5:37 am
by volka
Let's start with (more or less) what you have right now

Code: Select all

<pre><?php
$source = '...[nocode][b]text[/b][/nocode]...[nocode][i]text2[/i][/nocode]';
preg_match('|\[nocode\]([^]]*)\[/nocode\]|i', $source, $matchA);
print_r($matchA);
?></pre>
this pattern finds [nocode] and then tries to handle the next part - everything that is not ]. So, after this part is done, what's left?
...[nocode][b]text[/b][/nocode]...[nocode]text2[/nocode]
the next character is [ but the pattern needs a ] right now.
ok, next try

Code: Select all

<pre><?php
$source = '...[nocode][b]text[/b][/nocode]...[nocode][i]text2[/i][/nocode]';

preg_match('|\[nocode\]([^[]*)\[/nocode\]|i', $source, $matchA);
print_r($matchA);
?></pre>
doesn't work either
again the result looks like
...[nocode]text[/nocode]...[nocode]text2[/nocode]
now a [/nocode] must follow to make the pattern match but we're still at .

You see it's mandatory to tell PCRE you're looking for [nocode]...[/nocode]

Code: Select all

<pre><?php
$source = '...[nocode][b]text[/b][/nocode]...[nocode][i]text2[/i][/nocode]';

preg_match('|\[nocode](.*)\[/nocode]|i', $source, $matchB);

print_r($matchB);
?></pre>
now it's matching both nocode-blocks with one match, damn greedy PCRE ;)
but you can tell it to be less greedy, e.g. by preprending (?U) to the pattern you want to be handled ungreedy

Code: Select all

<pre><?php
$source = '...[nocode][b]text[/b][/nocode]...[nocode][i]text2[/i][/nocode]';

preg_match('|\[nocode](?U)(.*)\[/nocode]|i', $source, $matchC);

print_r($matchC);
?></pre>

Posted: Tue Jul 01, 2003 5:43 am
by Tubbietoeter
volka wrote:

Code: Select all

$source = '...[nocode][b]text[/b][/nocode]...[nocode][i]text2[/i][/nocode]';

preg_match('|\[nocode\](?U)(.*)\[\/nocode\]|i', $source, $matchC);

forgot to escape 2 brackets ...
and the slash /

;o)

Posted: Tue Jul 01, 2003 6:05 am
by volka
because there's no need to ;)
| is the delimeter so / has no special meaning in this pattern
pcre is smart enough to recognize \[ ] as content