Solved

string substitutions using PHP

Posted on 2003-12-11
3
232 Views
Last Modified: 2011-10-03
My immediate problem is that I want to change  a string in PHP. I want to change "somepattern" to "somepattern and some other stuff too".

I need the sed-type capability of putting "somepattern" back into the string. (sed uses the ( ) plus \1 \2 construct for this.)

Does PHP have this capability? I'm trying to use ereg_replace().
0
Comment
Question by:rfr1tz
  • 2
3 Comments
 
LVL 33

Accepted Solution

by:
snoyes_jw earned 250 total points
ID: 9921736
Yes, it can be done with ereg_replace or preg_replace.  Here is an example from http://www.php.net/ereg_replace :

>>>
For example, the following code snippet prints "This was a test" three times:
Example 1. ereg_replace() example
<?php
$string = "This is a test";
echo ereg_replace (" is", " was", $string);
echo ereg_replace ("( )is", "\\1was", $string);
echo ereg_replace ("(( )is)", "\\2was", $string);
?>  
<<<

See http://www.php.net/preg_replace for examples on using preg_replace.
0
 
LVL 3

Author Comment

by:rfr1tz
ID: 9930450
Geez, that ( ) syntax is awful.

In fact, what do those ( ) mean in "( )is" )?  defines "first" thing for use with \1, but
                                                                 this isn't obvious at all and then the
"(( ) is)"                                                    means the "second" thing for use with \2

I'm sorry. I see no pattern here at all. In sed, the parens are around the word like (is).
0
 
LVL 33

Expert Comment

by:snoyes_jw
ID: 9931051
you could also do something like str_replace($needle, $needle . $otherstuff, $haystack);
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

708 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now