0

My string is:

your string here, please/do it by yourself.

I want to remove the word before the slash(with the slash) and at the end to get this:

your string here, do it by yourself.

My code is:

$string = 'your string here, please/do it by yourself.';
$parsed = preg_replace("\s*\w+\s+(?:w/)", '', $string);

its not do anything (not even print the string without changes..)

Amanda
  • 21
  • 4

3 Answers3

1

Your regex has no delimiters, and just doesn't make sense...

"Zero or more spaces, one or more word characters, one or more spaces, followed by a w then a /"... huh?

$parsed = preg_replace("(\w+/)","",$string);
echo $parsed;
Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
  • You would have to escape it, if it's the delimiter. Otherwise `/` is just a character. – Fitzi Dec 11 '18 at 11:36
  • 1
    @executable The `()` are the delimiters so no the `/` is not a special character. `It is also possible to use bracket style delimiters where the opening and closing brackets are the starting and ending delimiter, respectively. (), {}, [] and <> are all valid bracket style delimiter pairs.`-http://php.net/manual/en/regexp.reference.delimiters.php – user3783243 Dec 11 '18 at 11:37
  • 1
    Parentheses have special meaning in context of regular expressions. Using them as delimiters just makes confusion. – revo Dec 11 '18 at 11:38
  • @revo Yes, I agree, I've also found it strange that PHP allows characters that have special meaning to be delimiters. – user3783243 Dec 11 '18 at 11:41
  • this example dont work with this character? (`|`) I try to do this code on Hebrew sentence and this character, here the sample: `שלום|היי` – Amanda Dec 11 '18 at 12:50
  • @revo Personally I use them because it reminds me that the entire match is always the 0th capture group. Definitely a personal preference there but since I've been doing it from the beginning it's what I'm used to. – Niet the Dark Absol Dec 11 '18 at 13:33
  • That's right but you (or OP) will later have a hard time escaping capturing groups, non-capturing groups, atomic groups, conditional clusters and other clusters and moreover what `\0` refers to isn't the 0th capturing group but the entire matching text. – revo Dec 11 '18 at 13:57
  • You don't have to escape capture groups, because `()` come in matching pairs. – Niet the Dark Absol Dec 11 '18 at 14:04
  • Interesting. I wasn't aware of it, although docs clearly state: *Bracket style delimiters do not need to be escaped when they are used as meta characters...* but that doesn't apply to angle brackets since you can't use `<>` as delimiters and benefit from a lookbehind or an atomic group because the including angle bracket doesn't have a matching pair and escaping in this case doesn't help either. I think this had to be documented. – revo Dec 12 '18 at 08:51
  • That does seem like an oversight, yes. `<>` delimiters would be awkward. – Niet the Dark Absol Dec 12 '18 at 14:25
1

An alternative solution not relying on regex:

<?php

$example = 'your string here, please/do it by yourself.';
$expected = 'your string here, do it by yourself.';

$slashPos = strpos($example, '/');
$spacePos = strrpos($example, ' ', -1 * $slashPos);

$string1 = substr($example, 0, $spacePos + 1); // Add 1 here to not remove space
$string2 = substr($example, (strlen($example) - $slashPos - 1) * -1); // Subtract 1 here to remove /

$result = $string1 . $string2;

var_dump(assert($expected === $result));

https://3v4l.org/XVruS

Output for 5.6.38 - 7.3.0

bool(true)

References:

http://php.net/manual/de/function.strpos.php

http://php.net/manual/de/function.strrpos.php

http://php.net/manual/de/function.substr.php

Community
  • 1
  • 1
Xatenev
  • 6,383
  • 3
  • 18
  • 42
0

What Niet the Dark Absol says. You need delimiters. Furthermore your regex (even with delimiters) would be saying: "0-infinite whitespaces followed by 1-infinite word-characters followed by 1-infinite spaces, followed by 'w/'".

That's not what you want. You want to remove the word before the slash - and the slash. You would need something like @\w+/@ (where the @ sybmols are the delimiters) and replace that by '', e.g. preg_replace('@\w+/@', '', $string); to replace any word followed by a slash by nothing.

minitauros
  • 1,920
  • 18
  • 20
  • Yes I realized after typing it. Have a bit more explanation about how the regex works, though. Might be useful to the topic starter. – minitauros Dec 11 '18 at 11:39