• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 164
  • Last Modified:

substituting a specified text in a file

from a file modifer.php I need to modify another file index.php substituting the text:
<div id=change this>foobar</div>

with this new text:
<div id=change this>abcdefghi</div>

what are the PHP commands to do this?
0
lucavilla
Asked:
lucavilla
  • 2
  • 2
2 Solutions
 
QualitySoftwareDevelopmentCommented:
It str_replace

e.g. from php.net
// Provides: <body text='black'>
$bodytag = str_replace("%body%", "black", "<body text='%body%'>");

you can also use $variable at the end instead of the <body ...
0
 
marchentCommented:

$newWord = "adsasdads";
$str = "<div id=change this>foobar</div>";
$str = preg_replace('|(<div id=change this>)foobar(</div>)|', "$1$newWord$2", $str);

Open in new window

0
 
lucavillaAuthor Commented:
marchent, what's the minimalist way to do the same?
0
 
marchentCommented:
don't know that.
0
 
QualitySoftwareDevelopmentCommented:
it really depends on how much you know of the existing string. If the <div id=change this> and </div> is always the same marchents pretty much the shortest way.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now