473,385 Members | 1,740 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Rexed replace with a list- Can I do this in PHP?

$ar=array("a","b","c");
$string="The third letter of the alpabet is %%2%% and the second is
%%1%%."
$string = preg_replace('/[%]{2}(.*?)[%]{2}/', '$ar[$1]', $string);
echo $string;

I want to see: "The third letter of the alphabet is c and the second
is b."
But what I see instead is: "The third letter of the alphabet is
$ar[$1] and the second is $ar[$1]." I tried double quotes, I tried no
quotes.

The php.net page for preg_replace has some examples in the feedback
that SAY to to this, but go try running them yourself and changing the
numbers- all they really doing is printing the first element, every
time. (In the above example they'd be the equivalent of:
$string = preg_replace('/[%]{2}(.*?)[%]{2}/', $ar[0], $string);)

In perl I could do this easy:
$string=~s/[%]{2}(.*?)[%]{2}/$ar{$1}/g;

How would I do this in PHP?
Jul 17 '05 #1
2 1697
mrbog wrote:
$ar=array("a","b","c");
$string="The third letter of the alpabet is %%2%% and the second is
%%1%%."
$string = preg_replace('/[%]{2}(.*?)[%]{2}/', '$ar[$1]', $string);
echo $string;

I want to see: "The third letter of the alphabet is c and the second
is b." How would I do this in PHP?


Use the /e modifier to evaluate the replaced part

$string = preg_replace('/[%]{2}(.*?)[%]{2}/e', '$ar[$1]', $string);
--
--= my mail box only accepts =--
--= Content-Type: text/plain =--
--= Size below 10001 bytes =--
Jul 17 '05 #2
Thanks much Pedro.
Jul 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: nnes | last post by:
I have really no mayor gripes, and the few things I would change would break backward compatibility, but here is the list anyway: 1.) Eliminate the whole long stuff. E.g. built-in long ()...
3
by: Rodrigo Benenson | last post by:
Hi, I have a weird need in tkinter. If I have a widget I can *set* a binding ..bind("<the event>", the_callback) I also can *append* a binding ..bind("<the event>", the_second_callback,...
2
by: s | last post by:
I'm getting compile errors on the following code: <code> #include <iostream> #include <fstream> #include <list> #include <string> using namespace std;
3
by: Dirk Hagemann | last post by:
Hi! I have a list of lists and in some of these lists are elements which I want to change. Here an example: lists= 'None' should be replaced by 0 or NULL or something else. But as far as I...
4
by: jgabbai | last post by:
Hi, What is the best way to white list a set of allowable characters using regex or replace? I understand it is safer to whitelist than to blacklist, but am not sure how to go about it. Many...
6
by: Lisa | last post by:
I am reading in data from a text file. I want to enter each value on the line into a list and retain the order of the elements. The number of elements and spacing between them varies, but a...
7
by: SpreadTooThin | last post by:
I want to replace an object in a list with a new object.... std::list<myObj>::iterator it; for (it=mylist.begin(); it != mylist.end(); ++it) { if (it->compair(myval) == 0) { *it = newval;...
7
by: Grok | last post by:
I need an elegant way to remove any characters in a string if they are not in an allowed char list. The part cleaning files of the non-allowed characters will run as a service, so no forms here. ...
5
oll3i
by: oll3i | last post by:
why when i try to make the string(unordered list) to be in a div it apears beside the div function makeUnorderedList(txt){ var tempText=""; var splitResult = txt.split('\n'); var...
2
by: antar2 | last post by:
I want to replace each first element in list 5 that is equal to the first element of the list of lists4 by the fourth element. I wrote following code that does not work: list4 = , , ] list5 = ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.