使用preg_replace在URL之前获得斜杠

时间:2013-06-07 06:54:52

标签: php preg-replace

使用preg_replace

在URL之前获得斜杠
$contentss = file_get_contents("http://www.ncbi.nlm.nih.gov/pubmed?LinkName=pubmed_pubmed&from_uid=18032633" ); 
preg_match('/<div class="rprt">(.*)<\/div>/',$contentss,$matches);

$patterns = '/pubmed/';

$replacements = 'http://ncbi.nlm.nih.gov/pubmed';

$string = '<a href="pubmed/"></a>';

$getreplacements = ( preg_replace($patterns, $replacements, $matches));

echo $getreplacements[0];

Out put在锚标记href中显示为 / http://ncbi.nlm.nih.gov/pubmed 。我想在http://ncbi.nlm.nih.gov/pubmed之前删除正斜杠。

请帮助解决这个问题。提前谢谢。

2 个答案:

答案 0 :(得分:0)

$patterns = '/pubmed/';替换为$patterns = '/\/pubmed/';

答案 1 :(得分:0)

将REGEX简单地更改为

$patterns = '/\/pubmed/';

以这种方式,它会将/pubmed更改为您的网址http://ncbi.nlm.nih.gov/pubmed

/需要在正则表达式中进行转义。

我已经测试了它并且它正在运行。