Current location: Hot Scripts Forums » Programming Languages » PHP » preg_replace problem (this title is getting boring)


preg_replace problem (this title is getting boring)

Reply
  #1 (permalink)  
Old 02-26-07, 08:30 AM
UnrealEd's Avatar
UnrealEd UnrealEd is offline
Community Liaison
 
Join Date: May 2005
Location: Antwerp, Belgium
Posts: 3,165
Thanks: 4
Thanked 25 Times in 25 Posts
preg_replace problem (this title is getting boring)

hi all,

as my title says, i'm facing a problem with preg_replace. The thing i'm working on is a simple phpbb mod (really simple): i want to change the color and the weight of certain words.
here's the sample i'm entering in the post field:
Quote:
class hello
Hello extends Hi
public abstract class Hello
public abstract class Hello extends Hi
public interface Hello
the output i want to get is this:
Quote:
class hello
Hello extends Hi
public abstract class Hello
public abstract class Hello extends Hi
public interface Hello
this is the code i'm using to do so:
PHP Code:

    $patterns = array();
    
$patterns[] = '(public\s)?(abstract\s)?(class|interface)?\s([A-Z][a-zA-Z0-9_]+)';
    
$patterns[] = $patterns[0].'\sextends\s([A-Z][a-zA-Z0-9_]+)';

    
$replaces = array();
    
$replaces[] = "[color=green:$uid][i:$uid]$1$2$3[/i:$uid][/color:$uid] [color=darkblue:$uid][b:$uid]$4[/b:$uid][/color:$uid]";
    
$replaces[] = $replaces[0]." [color=green:$uid][i:$uid]extends[/i:$uid][/color:$uid] [color=darkblue:$uid][b:$uid]$6[/b:$uid][/color:$uid]";
    for(
$i=0$i<sizeof($patterns); $i++)
        
$text preg_replace('#'.$patterns[$i].'#'$replaces[$i], $text); 
don't mind the $uid, it is set, and works fine. Now, my problem is that it all works except for the "extends" word: it just don't change to green, italic. My output looks like this:
Quote:
class hello
Hello extends Hi
public abstract class Hello
public abstract class Hello extends Hi
public interface Hello
when i display the $text var during the for loop, together with the $replace[$i] var, it says that "extends" is placed before the change of color code, like this:
Code:
extends[.color=green][.i][/i][/color]
anyone knows how to fix this?

thanx in advance,
UnrealEd
Reply With Quote
  #2 (permalink)  
Old 03-03-07, 01:59 AM
Patiek Patiek is offline
Wannabe Coder
 
Join Date: Nov 2003
Posts: 165
Thanks: 0
Thanked 0 Times in 0 Posts
I am tired (2:00 AM for me), but what is with the:
PHP Code:

$patterns[] = $patterns[0].'\sextends\s([A-Z][a-zA-Z0-9_]+)'
Why do you have "$patterns[0] ." there? I am not seeing *why* that should be there. Similar problem in the $replaces[].
__________________
EP-Dev (http://www.ep-dev.com)
- EP-Dev Whois Script
- EP-Dev Forum News
- EP-Dev Counter
- EP-Dev CMS (coming soon!)
Reply With Quote
  #3 (permalink)  
Old 03-05-07, 05:34 AM
UnrealEd's Avatar
UnrealEd UnrealEd is offline
Community Liaison
 
Join Date: May 2005
Location: Antwerp, Belgium
Posts: 3,165
Thanks: 4
Thanked 25 Times in 25 Posts
forget about it, i've solved it 5 days ago
the reason why it didn't work is because the $pattern[0] also matches the first part of $patterns[1], therefore it will replace the match, and $patterns[1] will return false

UnrealEd
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
First check the price, then check the title and see if contains.. Oskare100 PHP 1 01-04-07 03:53 PM
problem in excution of HTML Selection list unsing ASP sujata_ghosh ASP 2 05-17-06 11:59 PM
Classified Ads skipper23 Perl 3 11-22-05 02:22 AM
Problem creating dynamic title for anchors GO4TF4CE JavaScript 4 05-16-04 03:23 PM
Classified Ads skipper23 Perl 2 12-30-03 03:43 AM


All times are GMT -5. The time now is 05:57 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.