problem in replacing & form file_get_contents() page

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Mehnaz
Forum Newbie
Posts: 20
Joined: Mon Jun 02, 2008 7:49 pm

problem in replacing & form file_get_contents() page

Post by Mehnaz »

Hi

I am getting search results from a search engine in this script;

Code: Select all

 
 
<?php
 
$urlparam = $_GET['id'];
 
// get search results
$html = file_get_contents("http://vsearch.nlm.nih.gov/vivisimo/cgi-bin/query-meta?input-form=simple&v%3Asources=medlineplus-bundle&v%3Aproject=medlineplus&binning-state=&query=".$urlparam."&x=67&y=9");
 
$regex = "/<div class=\"document-header\"><a class=\"title\"(.+?) target=\"_/";
if (!preg_match_all($regex,$html,$matches))
    return false;
$matches=$matches[1]; 
 
for ($i=0;$i<count($matches);$i++) {
$match=trim($matches[$i]);
 $results[]= $match;
 
}
 
$expr = "/url=(.+?)rid/i";
 
foreach($results as $href) {
 
if (!preg_match($expr, $href, $tmpRes))
    return false;
    $urls[] = $tmpRes[1];
           
                      
           }
       
for ($i=0;$i<count($urls);$i++) {
$url=trim($urls[$i]);
$url=preg_replace ("/%3a%2f%2f/i","://",$urls[$i]);
$url= preg_replace ("/%2f/i","/",$url);
$url= preg_replace ("/%3f/","?",$url);
$url= preg_replace ("/%3d/","=",$url);
$url=preg_replace('/&/','',$url);
$url= preg_replace('/%26/','&',$url);
 $sresults[]= $url;
 
}           
print_r($sresults);
I have the problem in

Code: Select all

$url=preg_replace('/&/','',$url);
 
for the extracted url like this
Array ( [0] => http%3a%2f%2fwww.nlm.nih.gov%2fmedlineplus%2fcancer.html&

all other preg_replace works fine but the last & is repalced by 'amp'

in the output
Array ( [0] => http%3a%2f%2fwww.nlm.nih.gov%2fmedlineplus%2fcancer.htmlamp

I have tried str_replace but it does not work as

I am using php 5.2.6 on wamp 2.0

Please help

Thanks in advance

Mehnaz
Last edited by Benjamin on Mon May 18, 2009 8:34 pm, edited 1 time in total.
Reason: Changed code type from text to php.
MeLight
Forum Commoner
Posts: 26
Joined: Sun Apr 19, 2009 12:39 pm
Location: Israel

Re: problem in replacing & form file_get_contents() page

Post by MeLight »

I have tried str_replace but it does not work as
What do you mean? It won't run, won't give you the expected results, or what?
Post Reply