Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reg. exp. question 1

Status
Not open for further replies.

Masali

Programmer
Joined
Jun 19, 2002
Messages
143
Location
SE
Code:
$s="{This} is a {test} string";
preg_match_all ("/\{.*?\}/", $s, $output);
$re[0]="rep1";
$re[1]="rep2";
print preg_replace($output[0],$re,$s);

Why does this code print out:

{rep1} is a {rep2} string

instead of:

rep1 is a rep2 string

What is wrong?
 
Probably because preg_replace patterns are delimited by "/"..."/".

Take a look here:

Code:
<?php
$s=&quot;{This} is a {test} string&quot;;

preg_match_all (&quot;/\{.*?\}/&quot;, $s, $output);

foreach ($output[0] as $key => $value)
{
	$output[0][$key] = '/' . $value . '/';
}

$re = array (&quot;rep1&quot;, &quot;rep2&quot;);

print preg_replace($output[0],$re,$s);

?>

Want the best answers? Ask the best questions: TANSTAAFL!!
 
Ok, I get it!

Thanks alot!
 
Status
Not open for further replies.

Similar threads

  • Locked
  • Question Question
Replies
2
Views
1K
  • Locked
  • Question Question
Replies
2
Views
443

Part and Inventory Search

Sponsor

Back
Top