?
Solved

string substitutions using PHP

Posted on 2003-12-11
3
Medium Priority
?
239 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 1000 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

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

777 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