?
Solved

preg_replace  instead eregi_replace

Posted on 2010-01-07
5
Medium Priority
?
319 Views
Last Modified: 2012-05-11
Hi
I have this code
$main="we have {no} players ";  
$no=3;    
               
$template = eregi_replace("{no}", "$no", $main);

ECHO "$template";

// we have 3 players

How can i write it using preg_replace  instead eregi_replace (for php6 use)

thnaks
Andreas
0
Comment
Question by:adgram1
  • 3
5 Comments
 
LVL 12

Accepted Solution

by:
jet-black earned 2000 total points
ID: 26199277
You can just use str_replace for this. However, check that:

$main="we have {no} players ";  
$no=3;    
               
$template = preg_replace("/\{no\}/", "$no", $main);
0
 
LVL 4

Expert Comment

by:sudhakarsp06
ID: 26199311
$main="we have {no} playeno ";  
$no=3;    
               

$template = preg_replace("/{no}/", "$no", $main);

echo  "$template";
0
 
LVL 12

Expert Comment

by:jet-black
ID: 26199367
Actually, sudhakarsp06's code is wrong.
curly braces are special characters which are using for repetition and it may cause unexpected results.
http://www.regular-expressions.info/repeat.html

You should escape them.
0
 

Author Closing Comment

by:adgram1
ID: 31673963
Both of solutions where right , i gave the points to first answer
Thanks all
Andreas
0
 
LVL 12

Expert Comment

by:jet-black
ID: 26199413
@adgram1
Thanks for the points.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses four methods for overlaying images in a container on a web page
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses
Course of the Month17 days, 6 hours left to enroll

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question