...

View Full Version : What's wrong .. trouble with preg_replace



cyphix
05-12-2004, 12:46 AM
Getting a parse error in this code.. the part I'm getting it in is below the main 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..



$wb_rep[1] = '';


Any help?

cyphix
05-12-2004, 12:50 AM
Just FYI... the line..



$sw_pattern[1] = '/';


does have "/\" in it... but for some weird reason this forum isn't displaying it.

sad69
05-12-2004, 01:59 AM
Just FYI... the line..



$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:


$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:


$wb_pattern[1] = '/\\';


At least I think so.. let me know what happens.

Sadiq.

cyphix
05-12-2004, 11:30 AM
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. :confused:

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

cyphix
05-12-2004, 11:32 AM
Btw line 538 is..



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


548 is..



$swclean = preg_replace($sw_pattern, $sw_rep, $sw);


..& you can see the surrounding code above.

bcarl314
05-12-2004, 01:50 PM
So, what you want to do is replace "www." and "/" with blanks???

try this...



$string = "www.aol.com/junk";
$clean = preg_replace("/(w{3}\.)|\//","",$string);
echo $clean;


I think that should do it... hmm... looks ok.

cyphix
05-12-2004, 02:10 PM
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!

bcarl314
05-12-2004, 02:14 PM
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

cyphix
05-12-2004, 02:22 PM
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..



$wb = $web;

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

bcarl314
05-12-2004, 02:25 PM
The {3} means match the preceeding pattern (in this case the letter "w") exactly 3 times.

cyphix
05-12-2004, 02:25 PM
Thanks for the link. I'll check it out! :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum