Please could somebody tell me the regular expression in thefollowing case.
I want to replace variable within {} with some other variable in the string.
for e.g:-
$string = "{she} is my friend";
$replace_variable = "sita";
how to replace "{she}" with "sita" using regular expression?
9 1168
Why would you use a regular expression when the following will do and faster?
[php]str_replace('{she}', $replace_variable, $string);[/php]
Ronald :cool:
Can you please tell me how to use regular expression in this case as i am asked
to do using it?
Not very much different[php]preg_replace('/{she}/', $replace_variable, $string);[/php]
Ronald :cool:
thanx a lot for the quick reply :)
Can you also tell how to replace every thing in the string that starts and ends with curly braces with an array?
for e.g:
$string = " {this} chocolate belongs to {that} boy";
$replace_variable = array('that', 'this');
It will be of great help if you could reply soon.
YOU HAVE DOUBLE POSTED THIS QUESTION IN THE SAME THREAD!!! DO NOT DO THAT!! READ THE POSTING GUIDELINES BEFORE YOU CONTINUE.
Well I certainly do not know using a regular expression. That would be a large expression! Maybe someone else can help you here.
All I know is how to replace, using ARRAYS, strings in the str_replace as in the following example:
[php]// Provides: You should eat pizza, beer, and ice cream every day
$phrase = "You should eat fruits, vegetables, and fiber every day.";
$healthy = array("fruits", "vegetables", "fiber");
$yummy = array("pizza", "beer", "ice cream");
$newphrase = str_replace($healthy, $yummy, $phrase);[/php]
Ronald :cool:
This will helpful for replacing strings in the braces.
<?php
$string = " {this} chocolate belongs to {that} boy";
$pattern = "/{([^}]+)}/i";
preg_match_all($pattern,$string,$result);
$values = $result[1];
$replace_variable = array('rep1', 'rep2');
$newphrase = str_replace($values, $replace_variable, $string);
echo "<br> String : ".$string;
echo "<br> New String : ".$newphrase;
?>
I think the original questionn was how to replace the entire {string}, including braces, not just the value within the braces. This sort of replacement is usually done in most templates. So {this} must be replaced by rep1.
Ronald :cool:
This will replace the entire string,including the braces.
$string = " {this} chocolate belongs to {that} boy";
$pattern = "/({[^}]+})/i";
preg_match_all($pattern,$string,$result);
$values = $result[1];
$replace_variable = array('rep1', 'rep2');
$newphrase = str_replace($values, $replace_variable, $string);
echo "<br> String : ".$string;
echo "<br> New String : ".$newphrase;
Ranjith
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Kenneth McDonald |
last post by:
I'm working on the 0.8 release of my 'rex' module, and would appreciate
feedback, suggestions, and criticism as I work towards finalizing the
API and feature sets. rex is a module intended to make...
|
by: Buddy |
last post by:
Can someone please show me how to create a regular expression to do the
following
My text is set to
MyColumn{1, 100} Test
I want a regular expression that sets the text to the following...
|
by: Neri |
last post by:
Some document processing program I write has to deal with documents
that have headers and footers that are unnecessary for the main
processing part. Therefore, I'm using a regular expression to go...
|
by: Dimitris Georgakopuolos |
last post by:
Hello,
I have a text file that I load up to a string. The text includes
certain expression like {firstName} or {userName} that I want to match
and then replace with a new expression. However,...
|
by: James D. Marshall |
last post by:
The issue at hand, I believe is my comprehension of using regular
expression, specially to assist in replacing the expression with other text.
using regular expression (\s*) my understanding is...
|
by: Billa |
last post by:
Hi,
I am replaceing a big string using different regular expressions (see
some example at the end of the message). The problem is whenever I
apply a "replace" it makes a new copy of string and I...
|
by: Pete Davis |
last post by:
I'm using regular expressions to extract some data and some links from some
web pages. I download the page and then I want to get a list of certain
links.
For building regular expressions, I use...
|
by: Mike |
last post by:
I have a regular expression (^(.+)(?=\s*).*\1 ) that results in
matches. I would like to get what the actual regular expression is.
In other words, when I apply ^(.+)(?=\s*).*\1 to " HEART...
|
by: Allan Ebdrup |
last post by:
I have a dynamic list of regular expressions, the expressions don't change
very often but they can change. And I have a single string that I want to
match the regular expressions against and find...
|
by: NvrBst |
last post by:
I want to use the .replace() method with the regular expression /^ %VAR
% =,($|&)/. The following DOESN'T replace the "^default.aspx=,($|&)"
regular expression with "":...
|
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
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
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,...
|
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,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
| |