0

so I did

$subject = 'sakdlfjsalfdjslfad <a href="something/8230">lol is that true?</a> lalalala';

$subject = preg_replace('<a href="something\/([0-9]+)">(.+?)<\/a>', '$1', $subject);

echo $subject;

whereby the objective is to have $subject return

'sakdlfjsalfdjslfad lol is that true? lalalala'

but then PHP returned

warning: preg_replace(): Unknown modifier '(' 

what did I do wrong?

hakre
  • 193,403
  • 52
  • 435
  • 836
pillarOfLight
  • 8,592
  • 15
  • 60
  • 90

3 Answers3

1

the pattern needs delimiters -- slashes, e.g.

'/<a href="something\/([0-9]+)">(.+?)<\/a>/'
dldnh
  • 8,923
  • 3
  • 40
  • 52
1

You need delimiters around the pattern:

$subject = preg_replace('#<a href="something/([0-9]+)">(.+?)</a>#', '$1', $subject);
Toto
  • 89,455
  • 62
  • 89
  • 125
0

A PCRE (Perl Compatible Regular Expression) should be surrounded by delimiters, so

<a href="something\/([0-9]+)">(.+?)<\/a>

should be

/<a href="something\/([0-9]+)">(.+?)<\/a>/

I have used slashes (/) - but there are lots of choices

When using the PCRE functions, it is required that the pattern is enclosed by delimiters. A delimiter can be any non-alphanumeric, non-backslash, non-whitespace character.

Often used delimiters are forward slashes (/), hash signs (#) and tildes (~). The following are all examples of valid delimited patterns.

here are the docs for delimiters in pregex

Manse
  • 37,765
  • 10
  • 83
  • 108