开发者

delimiter error when preg_replace with arrays

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 开发者_如何学JAVA= 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 in preg_replace. class="title" is not a valid regular expression, since it has no valid delimiters. A valid regex version would be /class="title"/.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜