0

I have a PHP string in which I would like to find and replace using the strtr function, problem is I have variable fields so I won't be able to replace by name. The string contains tags like the following:

[field_1=Company]

[field_4=Name]

What makes it difficult is the "Company" and "Name" part of the "tag", these can be variable. So I basically looking for a way to replace this part [field_1] where "=Company" and "=Name" must be discarded. Can this be done?

To explain: I'm using "=Company" so users don't just see "field_1" but know the value it represents. However users are able to change the value to what they see fit.

7
  • This can be done with preg_replace and regular expressions. preg_replace manuals Commented Dec 21, 2017 at 16:21
  • @PhilippMaurer figured that, but I'm not an expert in regex field :-) Commented Dec 21, 2017 at 16:22
  • @OpoloWebdesign stackoverflow is not a programming service. Your problem is somewhat simple still. A good point to start working with regular expressions is regexone.com in my opinion. :-) Commented Dec 21, 2017 at 16:24
  • what is the output you want for this input after replacement Commented Dec 21, 2017 at 16:25
  • 2
    We are always glad to help and support new coders but you need to help yourself first. :-) After doing more research if you have a problem post what you've tried with a clear explanation of what isn't working and provide a Minimal, Complete, and Verifiable example. Read How to Ask a good question. Be sure to take the tour and read this. Commented Dec 21, 2017 at 16:26

1 Answer 1

1

You are probably looking for regular expressions. There is a function in PHP to do a regex replace:

http://php.net/manual/en/function.preg-replace.php

Been a while since I've worked in PHP but you might want to try something like this:

preg_replace('/field_\d/','REPLACEMENT','[field_1=Company]');

Should result in

[REPLACEMENT=Company]

If you want to replace everything except the brackets:

preg_replace('/field_\d+=\w+/','REPLACEMENT','[field_1=Company]');
Sign up to request clarification or add additional context in comments.

5 Comments

While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes.
Sorry I was working on some actual code but messed it up the first time, should be there now!
Thanks @Derek but I'm looking to find/replace using array called $user_input which contains keys like "field_1" (or "[field_1]") and values like "Something" and "Something here". I'm now getting a mismatch error: Warning: preg_replace(): Parameter mismatch, pattern is a string while replacement is an array in /var/www/html/pdf.php on line 39
@vespino It might help if you include your code attempt. But if you are trying to replace the key names in the array (if I am understanding correctly) you may need to loop through the array, run the preg_replace() on the key as a string, insert that new key and its value back into the array, and then delete (unset()) the original key/value element. array_map() might also be something to look at, but still not 100% I understand the situation.
You should be able to pass in arrays to both the first and second parameter to do what you want. Read the documentation, it provides details.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.