0

Not sure if this is the correct approach, but this is the only one that is coming to me right now, so new suggestions are welcome! I am trying to detect what a string says, and based on that, replace it with custom information inside a foreach.

foreach($customfields as $field)
{
    if($field->title == 'Date of Birth')
    {
        $title = str_replace($field->title, 'Date of Birth', 'Data urodzenia');
    }
    else if($field->title == 'Address Line 1')
    {
        // str_replace doesn't work, as the above has already been done, replacing each $field->title.
    }
    // rest of code
}

And then I display it in a table using $title. However, the issue is that obviously when one string is detected, they are all replaced, as each record gets replaced with the same string. How can I overcome/recode/reapproach this to make it work?

1
  • @zzwyb89- take a date of birth keyword in one variable..so you don't need to ifelse condition. only compare this with $field->title. Commented Dec 4, 2015 at 11:45

1 Answer 1

2

According to str_replace

mixed str_replace ( mixed $search , mixed $replace , mixed $subject [, int &$count ] )

The first 2 arguments could be arrays, so you can use it like:

$searches = array('Date of Birth', 'Address Line 1');
$replacements = array('Data urodzenia', 'Another address replacement');

$customFields = array_map(function($field) use ($searches, $replacements) {
    $field->title = str_replace($searches, $replacements, $field->title);
    return $field;
}, $customFields);

Also, the order you gave to the arguments is wrong, the string to do replacements into being the third one when calling the function.

For PHP versions lower than 5.3 the closures are not supported, so you can do the replacements inside the foreach loop as:

$searches = array('Date of Birth', 'Address Line 1');
$replacements = array('Data urodzenia', 'Another address replacement');

foreach($customfields as $field) {
    $field->title = str_replace($searches, $replacements, $field->title);
}
Sign up to request clarification or add additional context in comments.

7 Comments

Can this be used inside foreach? What if I'd like more than two arrays?
You don't need the foreach anymore.. only if you are doing other things to the fields.. but you can use the foreach after the code I provided
There's quite a bit I am doing in the foreach, such as generating form fields, and setting them classes. Placing your code before the foreach gives me Parse error: syntax error, unexpected T_FUNCTION, expecting ')'. Line 47 is the array_map(function.... line
What do you mean about more than 2 arrays? you can add how many searches and replacements you wish.. Although I recommend you to implement a localization system, because I suppose you are trying to localize the website
What version of PHP do you use? Closures (anonymous functions) were added in PHP 5.3.0
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.