Solved

string substitutions using PHP

Posted on 2003-12-11
3
234 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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.

809 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