1
votes

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

1
try setting back references around the expression /EXPRESSION_HERE/ - Samuel Cook

1 Answers

3
votes
$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