views:

27

answers:

1

Here is the code

<?php
$url='http://isrc.ulster.ac.uk';
$var = fread_url($url);// function calling to get the page from curl
$i=0;
$linklabel = array();
$linklabelmod = array();
$link = array();
$dom = new DOMDocument();
@$dom->loadHTML($var);
$xpath = new DOMXPath($dom);

foreach($xpath->query('//a') as $element) {
     $linklabel[] = $element->textContent;
     $link[] = $element->getAttribute("href");
     $i=$i+1;
}

for($k=0;$k<$i;$k++) {
    $linklabelmod[$k] = str_replace($linklabel[$k], $linklabel[$k]."[$k]", $linklabel[$k]);
    $var = preg_replace( "/\\Q$linklabel[$k]\\E/", $linklabelmod[$k], $var, 1 );//modifying link labels
}
print $var;
function fread_url($url){
    if(function_exists("curl_init")){
        $ch = curl_init();
        $user_agent = "Mozilla/4.0 (compatible; MSIE 5.01; "."Windows NT 5.0)";
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
        curl_setopt( $ch, CURLOPT_HTTPGET, 1 );
        curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
        curl_setopt( $ch, CURLOPT_FOLLOWLOCATION , 1 );
        curl_setopt( $ch, CURLOPT_FOLLOWLOCATION , 1 );
        curl_setopt( $ch, CURLOPT_URL, $url );
        curl_setopt ($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
        $html = curl_exec($ch);
        //print $html;//will printing the web page .
        curl_close($ch);
    }
    else{
        $hfile = fopen($url,"r");
        if($hfile){
            while(!feof($hfile)){
                $html.=fgets($hfile,1024);
            }
        }
    }
    return $html;
}
?> 

Not all link labels are changing. I want each link label to be modified by attaching a unique number. Plz run the code so that you can see error.. Thx in advance..

A: 

What about checking if a match was found before attempting to replace it? Using preg_match.

It is not my intention to ruin your question by asking this, but how would one reply to someone elses comment? I only see the 'add comment' on my own comments, thank you.

Chris
but i am a noob and don't know about implimenting specific preg_match expressions? plz help with an example....
Kiran George
`if ( preg_match( "/\\Q$linklabel[$k]\\E/", $var ) )` above your preg_replace call would most likely do.EDIT: Unless it is in fact complaining about the actual regexp syntax. If so, try something similar to `"/".$linkLabel[$k]."/i"`
Chris
no, its not working, here is the actual code...$dom = new DOMDocument();@$dom->loadHTML($var);$xpath = new DOMXPath($dom);foreach($xpath->query('//a') as $element) { $linklabel[] = $element->textContent; $link[] = $element->getAttribute("href"); $i=$i+1;}for($k=0;$k<$i;$k++) { $linklabelmod[$k] = str_replace($linklabel[$k], $linklabel[$k]."[$k]", $linklabel[$k]); if ( preg_match( "/\\Q$linklabel[$k]\\E/", $var ) ) $var = preg_replace( "/\\Q$linklabel[$k]\\E/", $linklabelmod[$k], $var, 1 );}print $var;//printing web page
Kiran George
@Chris problem solved, thx
Kiran George
Hehe, I'm glad you managed to solve it. I saw your response last night, but was in a rush and forgot about it when I got back home.
Chris