Jump to content

Recommended Posts

I'm really not sure why this does not work:

 

$tags = array(
            'TITLE',
            'NAMETO',
            'NAMEFROM',
            'MESSAGE',
        );

$data = array(
            'hello',
            'Steven',
            'Steven',
            'Hello',
        );

echo str_replace('{'.$tags.'}', $data, file_get_contents('emailmessage.html'));

 

The contents of emailmessage.html:

<html>

<head>

<title>{TITLE}</title>

</head>

<body>

<p>{NAMETO}, {NAMEFROM}, {MESSAGE}</p>

</body>

</html>

Should that not output an HTML page with the tags replaced? Currently it just outputs the HTML with nothing replaced.

Link to comment
https://forums.phpfreaks.com/topic/136020-solved-simple-string-replace/
Share on other sites

It must be a problem with multi-array-replacements so why not append to each array individually. So use the below:

<?
$tags = array(
            'TITLE',
            'NAMETO',
            'NAMEFROM',
            'MESSAGE',
        );

$data = array(
            'hello',
            'Steven',
            'Steven',
            'Hello',
        );
$id=0;
while (isset($tags[$id]))
    {
    $tags[$id]='{'.$tags[$id].'}';
    $id+=1;
    }
unset($id);
echo str_replace($tags, $data, file_get_contents('emailmessage.html'));
?>

May not exactly be the fastest but does the job.

It must be a problem with multi-array-replacements

 

str_replace accommodates mixed data type arguments (string, string | array, string | string, array | array, array).  There is no problem with multi-array-replacements.  The problem is that he put { } around the array, which is the same problem the code you provided is going to have, because you put {  } around your vars, too. 

 

There's only 2 reasons why there would be { }.  1, is if he was trying to encase the pattern in a delimiter. Since str_replace looks for an exact match, it does not use delimiters.  And even if he were to use for instance preg_replace, which does use delimiters, he still did it wrong.  He would need to put them around each individual array element in his $tags array, like so:

 

$tags = array(
            '{TITLE}',
            '{NAMETO}',
            '{NAMEFROM}',
            '{MESSAGE}',
        );

 

But again, str_replace does not use delimiters.  2nd thing is if he were trying to prevent ambiguity.  For instance, f you're using a variable inside quotes, for example:  "blahblah{$var}blahblah" but that doesn't work any more than trying to echo an entire array.  It will just echo 'array'

 

echo $tags; // output: array

echo "{$tags}blah"; // output: arrayblah

echo $tags[9]; // output: TITLE

echo "{$tags[0]}blah"; // output TITLEblah

 

Your code puts { } around each array element.  But to str_replace, that's not the same as doing

 

echo "{$tags[$x]}";

 

Though even if it were, it would be pointless, as there's nothing else in the string to create ambiguity.  What str_replace sees that as is "{TITLE}" so it looks for that literal string, which it won't find unless the document in question formats it like that.  Which now that I think about it, is a possibility, since he was obviously trying to do something with those brackets. 

 

If that is the case, instead of hardcoding an array and then turning around and looping through each element, just hardcode the {}'s in the array elements to begin with

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.