473,402 Members | 2,055 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,402 software developers and data experts.

ereg_replace problem

I have a string that has been saved in a database from a textarea form
field.

e.g.

$text = "Dear %name

The date is %date

yours,

%user"

I wish to parse the string replacing every word beginning with % with a
variable $name, $date etc.

However, whenever I use ereg_replace, it just finds the beginning of the
whole string, rather than individual words.. eg

ereg_replace('^', 'foo', $text);

returns "fooDear %name ...".

Any ideas?
Jun 27 '08 #1
5 1609
Hugh Oxford wrote:
I have a string that has been saved in a database from a textarea form
field.

e.g.

$text = "Dear %name

The date is %date

yours,

%user"

I wish to parse the string replacing every word beginning with % with a
variable $name, $date etc.

However, whenever I use ereg_replace, it just finds the beginning of the
whole string, rather than individual words.. eg

ereg_replace('^', 'foo', $text);

returns "fooDear %name ...".
What do _you_ think '^' means?
Also, don't use ereg_*, switch to preg_*

$string = preg_replace('/%([a-z]+)/sie','[you asked for $1]',$string);
--
Rik Wasmus
....moving deadlines for your pleasure.
Jun 27 '08 #2
Rik Wasmus wrote:
Hugh Oxford wrote:
>I have a string that has been saved in a database from a textarea form
field.

e.g.

$text = "Dear %name

The date is %date

yours,

%user"

I wish to parse the string replacing every word beginning with % with
a variable $name, $date etc.

However, whenever I use ereg_replace, it just finds the beginning of
the whole string, rather than individual words.. eg

ereg_replace('^', 'foo', $text);

returns "fooDear %name ...".

What do _you_ think '^' means?
Also, don't use ereg_*, switch to preg_*

$string = preg_replace('/%([a-z]+)/sie','[you asked for $1]',$string);
Erm, the /e doesn't belong there ('/%([a-z]+)/si'), I leave it to you to
define a desired callback (possibly with preg_replace_callback) to the
variable.
--
Rik Wasmus
....moving deadlines for your pleasure.
Jun 27 '08 #3
Rik Wasmus wrote:
Rik Wasmus wrote:
>Hugh Oxford wrote:
>>I have a string that has been saved in a database from a textarea
form field.

e.g.

$text = "Dear %name

The date is %date

yours,

%user"

I wish to parse the string replacing every word beginning with % with
a variable $name, $date etc.

However, whenever I use ereg_replace, it just finds the beginning of
the whole string, rather than individual words.. eg

ereg_replace('^', 'foo', $text);

returns "fooDear %name ...".

What do _you_ think '^' means?
Also, don't use ereg_*, switch to preg_*

$string = preg_replace('/%([a-z]+)/sie','[you asked for $1]',$string);

Erm, the /e doesn't belong there ('/%([a-z]+)/si'), I leave it to you to
define a desired callback (possibly with preg_replace_callback) to the
variable.
Thanks Rik.

From what I've understood ^ means match the beginning of the string. If
I do ereg_replace('^%', 'foo', $text); it doesn't find anything at all.
Jun 27 '08 #4
Hugh Oxford wrote:
Rik Wasmus wrote:
>Rik Wasmus wrote:
>>Hugh Oxford wrote:
I have a string that has been saved in a database from a textarea
form field.

e.g.

$text = "Dear %name

The date is %date

yours,

%user"

I wish to parse the string replacing every word beginning with %
with a variable $name, $date etc.

However, whenever I use ereg_replace, it just finds the beginning of
the whole string, rather than individual words.. eg

ereg_replace('^', 'foo', $text);

returns "fooDear %name ...".

What do _you_ think '^' means?
Also, don't use ereg_*, switch to preg_*

$string = preg_replace('/%([a-z]+)/sie','[you asked for $1]',$string);

Erm, the /e doesn't belong there ('/%([a-z]+)/si'), I leave it to you
to define a desired callback (possibly with preg_replace_callback) to
the variable.

Thanks Rik.

From what I've understood ^ means match the beginning of the string. If
I do ereg_replace('^%', 'foo', $text); it doesn't find anything at all.
^ indeed matches the beginning of the string, meaning the string you
provide, not 'beginning of a seperate word'. A $string=
preg_replace('/^/','foo',$string); is the same as saying: $string =
'foo'.$string;. (You can alter this behaviour to make it also valid for
start of a line) An '/^%/' would only be found if you provided $string's
first character is '%'.

You are most likely looking for \b (word boundary)
--
Rik Wasmus
....moving deadlines for your pleasure.
Jun 27 '08 #5
Rik Wasmus wrote:
>
You are most likely looking for \b (word boundary)
Thanks Rik for all your help. It is appreciated. I used your code and
wrote a callback function.
FFR, to use an object method as a callback function you need to use
array($this, 'function_name') as the callback function.
Jun 27 '08 #6

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

Similar topics

1
by: Roland Sippel | last post by:
Is there anything change in "ereg_replace()" PHP > 4.3.2. Scripts works in PHP 4.3.1 but NOT in PHP 4.3.2: $text = "<!-- test -->"; $text = ereg_replace("]+"," ",strip_tags($text)); echo $text...
2
by: Chris | last post by:
Hello, I use a javascript ticker in my header which contains the news in a array like that tickercontents='Message1' tickercontents='Message2' tickercontents='Message3' now i want to write an...
4
by: knocte | last post by:
Hello. I am testing this curious code and I don't understand yet why it doesn't work with the 2nd case: Case 1: $number = "2,4"; $number = ereg_replace(",",".",$number); echo $number; //...
0
by: Sean Pinto | last post by:
i am designing a search engine for our website. i want to allow for searchablity based on partial string match as well as quoted exact matches. for example the search search text many would...
3
by: Xaradas | last post by:
Someone could tell me why preg_replace eat a slash when it do replacement? <? $stringToReplace = "blah blah {nome} blah blah"; $replacement = "Two slash: \\\\"; $stringToReplace =...
0
by: pieterprovoost | last post by:
Hi, I would like to use the ereg_replace() function to replace wiki-like syntax, for example: ] becomes <a href="index.php?page=somepage">somepage</a> **hello** becomes <i>hello</i>
1
by: tmcp | last post by:
Hello I'm a bit new to regular expressions and I'm having a problem I'm trying to strip any code which is inside a <p> tag for instance from <p style="margin: 0in 0in 0pt;" class="MsoNormal"> to...
2
by: encepif | last post by:
I think this is the right command., could someone please show me how to replace instances of a quote like this " with its html version - &quot;. I am mixed up with the escaping, etc. Thank you :-) ...
6
by: chromis | last post by:
Hi, I'm trying to remove line breaks from a user-inputted string and trying to do this using ereg_replace and a regular expression but struggling, hope someone can help! I would like to make...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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...
0
tracyyun
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
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 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.