Solved

string substitutions using PHP

Posted on 2003-12-11
3
237 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
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 a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

732 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