preg_replace/eregi_replace issue

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

preg_replace/eregi_replace issue

Post 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);
Tubbietoeter
Forum Contributor
Posts: 149
Joined: Fri Mar 14, 2003 2:41 am
Location: Germany

Post by Tubbietoeter »

i believe you need to quote the brackets [ or ] your looking for.
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

Code: Select all

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

Code: Select all

'\&#1111;nocode]&#1111;^]]*&#1111;\/nocode]'
User avatar
forgun
Forum Commoner
Posts: 61
Joined: Wed Jan 29, 2003 6:05 am
Contact:

Post by forgun »

what that mean

Code: Select all

'\&#1111;nocode]&#1111;^]]*&#1111;\/nocode]'
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post 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. :)
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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>
Tubbietoeter
Forum Contributor
Posts: 149
Joined: Fri Mar 14, 2003 2:41 am
Location: Germany

Post 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)
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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
Post Reply