0

I am pulling variables from url

$v1 = $_GET[‘v1’]

I am then trying to string replace them but having difficulty getting what I want…

  • I want - (single dash) to stay - (single dash)
  • I want —- (dash dash) to be replace by (single space)
  • I want ——- (dash dash dash) to be replaced by - (space dash space)

I’ve tried the following in the past, but all 3 get replaced by single space every time. Not sure how to revise this to accomplish what I am aiming for this time.

$name = str_replace('--','- ',str_replace('---',' - ',str_replace('-',' ',$v1)));

  • You will never have three dashes, if you replace a single dash to space in the very first replacement ;) – Honk der Hase Oct 16 '22 at 21:31
  • It can happen in any order in the coding..I was just specifying what I was after as a whole. – United Pro Source Oct 16 '22 at 21:52
  • But it's important to do it in the _right_ order... As I said, if you replace any single dash with a space, you remove all dashes from the string... after that, there won't be double or triple dashes in there... – Honk der Hase Oct 16 '22 at 21:56
  • I was listing out requirements in the code, not order of operations. Common sense tells you that if I want something to replace three dashes that would trigger first. Of course order matters in the actual code. – United Pro Source Oct 16 '22 at 22:03
  • The code example was something I grabbed from somebody else and could never modify it to do what I was after – United Pro Source Oct 16 '22 at 22:10
  • @UnitedProSource Please provide a complete battery of sample strings that cover all reasonable fringe cases and show us your exact desired result for each string. How close is this? https://phpize.online/sql/mysql57/undefined/php/php81/959e01e8c074a1614d6b420a007ab2be/ – mickmackusa Oct 16 '22 at 23:24
  • Your response to Lessmore's now-deleted answer indicates that you are not doing a great job of expressing your specific scenario. – mickmackusa Oct 16 '22 at 23:25
  • First rule is obsolete (dash should stay dash.. eg. "do nothing"). Replace 3 dashes with space-dash-space first, then replace double dash by space. That's it. – Honk der Hase Oct 16 '22 at 23:41
  • @Honk There was already an answer that implements that logic. The asker commented under that answer to indicate that that approach is not suitable for their project. We need clarification. They said: "_This doesn’t accomplish what I am after in bullet points._" – mickmackusa Oct 16 '22 at 23:48
  • Oh... didn't realized that. – Honk der Hase Oct 16 '22 at 23:49
  • Because his answer didn’t solve it. If you look at his example and his end solution, it wasn’t what I was asking for in my bullet points. Which is why he took his answer down. But I figured out the solution before Honk suggested it or I would have asked him to post it as an answer. – United Pro Source Oct 17 '22 at 00:23
  • I recommend strtr() as indicated in my sandbox link. [How to replace multiple substrings without replacing replaced substrings?](https://stackoverflow.com/a/44355722/2943403) although this task can be implemented with a single call of `str_replace()` with an array of search and an array of replacement parameters. – mickmackusa Oct 17 '22 at 00:42
  • This page is suitably closed with this dupe target: [Str_replace for multiple items](https://stackoverflow.com/q/7605480/2943403) @Honk (I already voted to close as Unclear, so I can no longer use my hammer.) – mickmackusa Oct 17 '22 at 00:43
  • You should use [`strtr`](https://www.php.net/manual/en/function.strtr.php): `strtr($v1, array('-' => '-', '--' => ' ', '---' => ' - '))` – Nick Oct 17 '22 at 00:59

1 Answers1

0

It is amazing how simple this is. They first requirement solves itself, so all I needed to do was focus on the other 2 bullet points.

$name = str_replace('--',' ',str_replace('---',' - ',$v2));