Page 1 of 1

Php and preg_replace help!? (solved)

Posted: Sat Feb 04, 2006 7:16 pm
by burnthewitches
Could you please tell me what's wrong with this? :(
I've spend 3 hours on it... this should NOT happen! o_O

Code: Select all

$test = array('test');
$test = preg_replace("/(.*)/", "/\\1/i", $test);
echo $test[0];
it should echo

Code: Select all

/test/i
... instead it echos

Code: Select all

/test/i//i
am I missing something? o_O
I can't take this any more I'm starting to lose it! :P

please help!?

Posted: Sat Feb 04, 2006 7:20 pm
by Gambler
Maybe

Code: Select all

$test = preg_replace("/(.*)/", "/$1/i", $test);
?

Posted: Sat Feb 04, 2006 7:30 pm
by feyd
if this is the end goal, you're wasting a lot of time starting up the regex engine just for this.

Anyways, there's some weirdness going on with the use of double quotes and not matching full lines. The following works

Code: Select all

[feyd@home]>php -r "$test = array('test'); $test = preg_replace('/^(.*)$/', '/\\1/i', $test); var_export($test);"
array (
  0 => '/test/i',
)

Posted: Sat Feb 04, 2006 8:03 pm
by burnthewitches
My goal is to wrap every row in an array in / and /i
how should I do it? loop?

thanx a million for your reply! :)

Posted: Sat Feb 04, 2006 8:49 pm
by feyd
a loop works, and so does using array_map():

Code: Select all

function patternize($input) {
  return '/'.$input.'/i';
}

$test = array('test','42','1816ape');
$test = array_map('patternize',$test);
var_export($test);