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)));

14
  • You will never have three dashes, if you replace a single dash to space in the very first replacement ;) Commented Oct 16, 2022 at 21:31
  • It can happen in any order in the coding..I was just specifying what I was after as a whole. Commented Oct 16, 2022 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... Commented Oct 16, 2022 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. Commented Oct 16, 2022 at 22:03
  • The code example was something I grabbed from somebody else and could never modify it to do what I was after Commented Oct 16, 2022 at 22:10

1 Answer 1

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));

Sign up to request clarification or add additional context in comments.

1 Comment

This question is missing its minimal reproducible example. We don't know if $v1 is a string or an array of strings.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.