Forum Moderators: coopster

Message Too Old, No Replies

replace arrays

         

WhosAWhata

11:04 pm on Apr 20, 2004 (gmt 0)

10+ Year Member



i have code like this,

$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?

WhosAWhata

11:06 pm on Apr 20, 2004 (gmt 0)

10+ Year Member



it also messes up words...

old:
hi. Whats up? this shouldn't change

new:
hello. how are you? thellos shouldn't change

ergophobe

3:29 pm on Apr 21, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



* ereg_replace doesn't support arrays, but preg_replace does. Remember that for preg_* delimiters are /. Use the i flag for case-insensitive searches.

* 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

WhosAWhata

3:06 am on Apr 24, 2004 (gmt 0)

10+ Year Member



a little help is required with the preg_replace

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]

ergophobe

1:55 pm on Apr 24, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



If I understand, your directories have a 3char month name and a 2-digit year. In that case, the search string I gave you will not work, because it assumed that you were trying to find words as in the first example you gave. the first '\b' will fail to match and so no replacement will take place.

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/";

WhosAWhata

2:47 pm on Apr 24, 2004 (gmt 0)

10+ Year Member



thanks i was able to get it to work after a little tinkering