I want to preg_replace some strings in an html file but the code below gives me error
Warning: preg_replace() [function.preg-replace]: Delimiter must not be
alphanumeric or backslash in /wamp/www/example.php on line 12
Line 12 is the echo preg_replace($matches[0], $results, $sourcestring);
$sourcestring=file_get_contents("example.html");
preg_match_all('/(?<=\/)\d+(?=\-)/',$sourcestring,$matches);
$results = array_unique($matches[0], SORT_NUMERIC);
$results = array_values($results);
for($i=0; $i<count($results); $i++) {
$results[$i] = 'id="news-id-'.$results[$i].'"';
}
preg_match_all('/class="title"/', $sourcestring, $matches);
echo preg_replace($matches[0], $results, $sourcestring);
Your
$matches[0]value will be (something like)class="title". You’re using that as a regular expression inpreg_replace.class="title"is not a valid regular expression, since it has no valid delimiters. A valid regex version would be/class="title"/.