Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

string substitutions using PHP

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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

604 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