Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    Regular Coder
    Join Date
    Apr 2004
    Posts
    682
    Thanks
    24
    Thanked 1 Time in 1 Post

    What's wrong .. trouble with preg_replace

    Getting a parse error in this code.. the part I'm getting it in is below the main code..

    PHP Code:
    $wb $web;

    $wb_pattern[0] = 'www.';
    $wb_pattern[1] = '/\';

    $wb_rep[1] = '';
    $wb_rep[0] = '';

    $wbclean = preg_replace($wb_pattern, $wb_rep, $wb);

    $sw = $searchweb;

    $sw_pattern[0] = '
    www.';
    $sw_pattern[1] = '
    /';

    $sw_rep[1] = '';
    $sw_rep[0] = '
    ;

    $swclean preg_replace($sw_pattern$sw_rep$sw); 
    This is the line it reports it on..

    PHP Code:
    $wb_rep[1] = ''
    Any help?
    Last edited by cyphix; 05-11-2004 at 11:49 PM.

  • #2
    Regular Coder
    Join Date
    Apr 2004
    Posts
    682
    Thanks
    24
    Thanked 1 Time in 1 Post
    Just FYI... the line..

    PHP Code:
    $sw_pattern[1] = '/'
    does have "/\" in it... but for some weird reason this forum isn't displaying it.

  • #3
    Senior Coder
    Join Date
    Feb 2004
    Posts
    1,206
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by cyphix
    Just FYI... the line..

    PHP Code:
    $sw_pattern[1] = '/'
    does have "/\" in it... but for some weird reason this forum isn't displaying it.
    Do you mean "\/" or "/\"?

    Anyway, in this code:
    PHP Code:
    $wb_pattern[1] = /; 
    You're escaping the closing single-quote. So you haven't terminated that string.. I'm not sure what that string is supposed to hold but if it's supposed to hold /\ in quotes, then you need to escape the \ so it should read:
    PHP Code:
    $wb_pattern[1] = '/\\'
    At least I think so.. let me know what happens.

    Sadiq.

  • #4
    Regular Coder
    Join Date
    Apr 2004
    Posts
    682
    Thanks
    24
    Thanked 1 Time in 1 Post
    Ah yeah. I was getting confused on how to escape slashes & if you escpaped them the same way you do ".

    They both should look like this..

    /

    so escaped would look like this..

    \/

    Anyway.. I changed them both to "\/" but still getting errors.. which is followed by the correct search result display.

    Warning: Delimiter must not be alphanumeric or backslash in xxxxxxxxxxxxx/search.php on line 538

    Warning: Delimiter must not be alphanumeric or backslash in xxxxxxxxxxxxx/search.php on line 538

    Warning: Delimiter must not be alphanumeric or backslash in xxxxxxxxxxxxx/search.php on line 548

    Warning: Delimiter must not be alphanumeric or backslash in xxxxxxxxxxxxx/search.php on line 548

  • #5
    Regular Coder
    Join Date
    Apr 2004
    Posts
    682
    Thanks
    24
    Thanked 1 Time in 1 Post
    Btw line 538 is..

    PHP Code:
    $wbclean preg_replace($wb_pattern$wb_rep$wb); 
    548 is..

    PHP Code:
    $swclean preg_replace($sw_pattern$sw_rep$sw); 
    ..& you can see the surrounding code above.

  • #6
    Mega-ultimate member
    Join Date
    Jun 2002
    Location
    Winona, MN - The land of 10,000 lakes
    Posts
    1,855
    Thanks
    1
    Thanked 45 Times in 42 Posts
    So, what you want to do is replace "www." and "/" with blanks???

    try this...

    PHP Code:
    $string "www.aol.com/junk";
    $clean preg_replace("/(w{3}\.)|\//","",$string);
    echo 
    $clean
    I think that should do it... hmm... looks ok.

  • #7
    Regular Coder
    Join Date
    Apr 2004
    Posts
    682
    Thanks
    24
    Thanked 1 Time in 1 Post
    Thanks for the code.... I'll try it!

    Also, Would you mind telling me how that works so I can understand it for next time I wanna do some replacements?

    Thanks!

  • #8
    Mega-ultimate member
    Join Date
    Jun 2002
    Location
    Winona, MN - The land of 10,000 lakes
    Posts
    1,855
    Thanks
    1
    Thanked 45 Times in 42 Posts
    Sure, the best I've got is a few lecture slides on regular expressions.

    Check these out...

    http://www.aphion.com/class/class11/intro.html

  • #9
    Regular Coder
    Join Date
    Apr 2004
    Posts
    682
    Thanks
    24
    Thanked 1 Time in 1 Post
    Ok, that works..

    Plus I think I understand it now anyway heh... just don't know how the (3) works, but I gather it just adds 3 w's but don't know how it works.

    I even modified it & added in "http:" & I got it working heh..

    PHP Code:
    $wb $web;

    $wbclean preg_replace("/(w{3}\.)|\/|(http:)/","",$wb); 

  • #10
    Mega-ultimate member
    Join Date
    Jun 2002
    Location
    Winona, MN - The land of 10,000 lakes
    Posts
    1,855
    Thanks
    1
    Thanked 45 Times in 42 Posts
    The {3} means match the preceeding pattern (in this case the letter "w") exactly 3 times.

  • #11
    Regular Coder
    Join Date
    Apr 2004
    Posts
    682
    Thanks
    24
    Thanked 1 Time in 1 Post
    Thanks for the link. I'll check it out!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •