Php and preg_replace help!? (solved)

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
User avatar
burnthewitches
Forum Newbie
Posts: 4
Joined: Sat Feb 04, 2006 7:14 pm

Php and preg_replace help!? (solved)

Post 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!?
Last edited by burnthewitches on Sun Feb 05, 2006 10:54 am, edited 1 time in total.
Gambler
Forum Contributor
Posts: 246
Joined: Thu Dec 08, 2005 7:10 pm

Post by Gambler »

Maybe

Code: Select all

$test = preg_replace("/(.*)/", "/$1/i", $test);
?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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',
)
User avatar
burnthewitches
Forum Newbie
Posts: 4
Joined: Sat Feb 04, 2006 7:14 pm

Post 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! :)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

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