1

I would like to replace a string like

title="CONTSTANTWORD fnwif 740933840 j iowej902 ijofiowi CONTSTANTWORD"

with

id="detectLink"

The word CONTSTANTWORD is, surprisingly, constant. The rest,

fnwif 740933840 j iowej902 ijofiowi 

is variable.

I found this answer, so I edited the code to this:

$html = preg_replace('title="CONTSTANTWORD (.*) CONTSTANTWORD"','id="detectLink"',$string);

But unfortunately, this doesn't work. Does anyone know what my preg_replace code should look like?

Edit: this is the error I get:

Warning: preg_replace() [function.preg-replace]: Delimiter must not be alphanumeric or backslash in ..... on line 8

Community
  • 1
  • 1
Carrot
  • 187
  • 1
  • 1
  • 8
  • 1
    try setting back references around the expression `/EXPRESSION_HERE/` – Samuel Cook Feb 21 '14 at 16:32
  • possible duplicate of [PHP using preg\_replace : "Delimiter must not be alphanumeric or backslash" error.](http://stackoverflow.com/questions/2527657/php-using-preg-replace-delimiter-must-not-be-alphanumeric-or-backslash-error) – Toto Feb 22 '14 at 10:31

1 Answers1

3
$test = 'title="CONTSTANTWORD fnwif 740933840 j iowej902 ijofiowi CONTSTANTWORD"';
$html = preg_replace('#title="CONTSTANTWORD (.*) CONTSTANTWORD"#','id="detectLink"',$test);
var_dump($html);

Outputs

string(15) "id="detectLink""

You need to put a delimiter around your regex

exussum
  • 18,275
  • 8
  • 32
  • 65