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.

vespino
  • 1,714
  • 3
  • 15
  • 28
  • This can be done with `preg_replace` and regular expressions. [preg_replace manuals](http://php.net/manual/en/function.preg-replace.php) – Philipp Maurer Dec 21 '17 at 16:21
  • @PhilippMaurer figured that, but I'm not an expert in regex field :-) – vespino Dec 21 '17 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](https://regexone.com/) in my opinion. :-) – Philipp Maurer Dec 21 '17 at 16:24
  • what is the output you want for this input after replacement – Alive to die - Anant Dec 21 '17 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**](https://meta.stackoverflow.com/q/261592/1011527) 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](http://stackoverflow.com/help/mcve). Read [How to Ask](http://stackoverflow.com/help/how-to-ask) a good question. Be sure to [take the tour](http://stackoverflow.com/tour) and read [this](https://meta.stackoverflow.com/q/347937/1011527). – Jay Blanchard Dec 21 '17 at 16:26
  • So which part of the string you want kind of lost you in your explanation – pr1nc3 Dec 21 '17 at 16:26
  • @pr1nc3 I would like to replace [field_1=Company] as a whole by "something" which is in an array "field_1" => "Something", "field_2" => "Name". – vespino Dec 21 '17 at 16:28

1 Answers1

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]');
Derek
  • 1,196
  • 11
  • 32
  • 2
    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. – Jay Blanchard Dec 21 '17 at 16:26
  • Sorry I was working on some actual code but messed it up the first time, should be there now! – Derek Dec 21 '17 at 16:31
  • 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 Dec 21 '17 at 16:35
  • 1
    @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. – C Miller Dec 21 '17 at 17:28
  • 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. – Derek Dec 21 '17 at 17:49