Forum Moderators: coopster
$text = "hi. Whats up? this shouldn't change";
$old = array('hi','whats up');
$new = array('hello','how are you');
$ntext = eregi_replace($old,$new,$text);
it doesn't actually change anything, i know it could be solved like this:
$text = "hi. Whats up? this shouldn't change";
$old = array('hi','whats up');
$new = array('hello','how are you');
$ntext = $text;
foreach($old as $i => $val) {
$ntext = eregi_replace($val,$new[$i],$ntext);
}
but is there a better way to do this?
* what's happening in the words is right. You need to define the word boundary with the \b or the [^\w].
so
$text = "hi. What's up? this shouldn't change";
$old = array('/\bhi\b/i','/\bwhat\'s up\b/i');
$new = array('hello','how are you');
$ntext = preg_replace($old,$new,$text);
(note I put a ' in the phrase (What's), just for demonstration purposes).
Tom
i have this code
<?
$dir = "./archives/";
$d = dir($dir);
while ($f = $d->read()) {
if(is_dir($dir."/".$f) && ($f!= ".") && ($f!= "..")) {
$files[] = $f;
}
}
$old = array('/\bjan\b/i','/\bfeb\b/i','/\bmar\b/i','/\bapr\b/i','/\bmay\b/i','/\bjun\b/i','/\bjul\b/i','/\baug\b/i','/\bsep\b/i','/\boct\b/i','/\bnov\b/i','/\bdec\b/i');
$new = array('january','february','march','april','may','june','july','august','september','october','november','december');
foreach($files as $n => $month) {
$month = ucwords($month);
$c[$n] = $month;
preg_replace($old,$new." 20",$c[$n]);
$c[$n] = ucwords($c[$n]);
$link[$n] = "<a href=\"".$month."\">".$c[$n]."</a>";
}
$links=count($link);
$a=1;
echo "<table align=center>";
for($i=0;$i<=$links;$i++){
if($a == "1") { echo "\n<tr>"; }
echo "<td>".$link[$i]."</td>";
if($a == "5") { echo "</tr>"; $a = 0; }
$a++;
}
if($a!= "1") { echo "</tr>"; }
echo "</table>";
?>
in the "archives" dir i have like Jan04/ Feb04/ and Mar03/
this is supposed to output (changed line breaks to make easier to read)
<table align=center>
<tr>
<td><a href="Jan04">January 2004</a></td>
<td><a href="Feb04">February 2004</a></td>
<td><a href="Mar03">March 2003</a></td>
</tr></table>
but instead i get (changed line breaks to make easier to read)
<table align=center>
<tr>
<td><a href="Jan04">Jan04</a></td>
<td><a href="Feb04">Feb04</a></td>
<td><a href="Mar03">Mar03</a></td>
</tr></table>
why?
[edit]Removed /\b from $new array, didn't help[/edit]
Also, the $new . "20" doesn't really mean anything. I'm surprised you don't get a parse error. PHP has so many weird little constructs, but AFAIK you can't just concatenate as a sort of callback function and have that appended to each string.
If your data is genuinely going to look like "Jan04" with exactly that format and you want it to output January 2004" you need to have something like this
$pattern = "/Jan(\d\d)/";
$replace = "/January 20$1/";