• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 342
  • Last Modified:

Regular Expression str_replace, prg_match or preg_match_all

Hi Experts

I am fetching a url content of a page... need to replace all url addresses in the page that matches this

this url http://blah.uni.edu.au/page/cgi-bin/extinfo.cgi?type=1&host=blah

with this one http://new.url.com/blah

I am doing it this way...Need help with rest

<?php
$fp=fopen("http://username:password@blah.uni.edu.au/page/cgi-bin/status.cgi","r");
$buffer = fread ($fp, 10000000);
$string = "http://blah.uni.edu.au/page/cgi-bin/extinfo.cgi?type=1&host=blah";
$new="http://new.url.com/blah";

Need help here....

echo $NewPage;
?>

Thank you in advance
0
baz_can_fix_it
Asked:
baz_can_fix_it
  • 6
  • 3
2 Solutions
 
BatalfCommented:
If the $string is a constant, you could use a simple str_replace function after modifiying the $string and $new variable slightly:

$string = "http://blah.uni.edu.au/page/cgi-bin/extinfo.cgi?type=1&host=";
$new = "http://new.url.com/";

$buffer = str_replace($string,$new,$buffer);

Batalf
0
 
Daniel_HegenerCommented:
If you want to rewrite
http://BLAH.uni.edu.au/page/cgi-bin/extinfo.cgi?type=1&host=blah

into
http://new.url.com/BLAH/page/cgi-bin/extinfo.cgi?type=1&host=blah

try the following (i have tested it with javascript but not with php):

<?php
$string = "http://blah.uni.edu.au/page/cgi-bin/extinfo.cgi?type=1&host=blah";
$pattern = "/http:\/\/(\w+)\.([\w\.]*\/)(.*)/i";
$replacement = "http://new.url.com/\$1\$3";
echo preg_replace($pattern, $replacement, $string);
?>

Read the following two pages carefully in order to use regular expressions:

http://de3.php.net/manual/de/function.preg-replace.php
http://de3.php.net/manual/de/pcre.pattern.syntax.php
0
 
baz_can_fix_itAuthor Commented:
Hi batalf

Sorry that doesn't work....


Hi Daniel_Hegener

With yours i just get this on the screen

http://new.url.com/nocnetsaint/cgi-bin/extinfo.cgi?type=1&host=


I am trying to produce the same page that I am feching with the New URL, So when a user goes to my sight to this page they see the other page except with my links embeded in there...


Cheers
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
baz_can_fix_itAuthor Commented:
hehehe Site not Sight...excuse the spellings...

cheers
0
 
Daniel_HegenerCommented:
Can you please exactly write down which part of the url you want to replace with what?!!! And which parts of the old url have to appear where in the new url?

If you want replace only the one part with another without using substrings of the old url in your new url (like e.g. "BLAH" in my last example) there is no need for using regular expressions.

Try Batalf's solution instead in a slightly different way:

$string = "http://blah.uni.edu.au";
$new = "http://new.url.com";

$buffer = str_replace( $string, $new, $buffer );

This will replace all occurences of $string with $new.
0
 
Daniel_HegenerCommented:
In the last case you could also replace all occurrences of "http://blah.uni.edu.au" with "" (with an empty string) in order to use relative links.

Oh, i just notice the ".au" in your url. If you're from Austria, try talking german to me if this might help, hehe, Alter, ich komm' aus Deutschland...
0
 
baz_can_fix_itAuthor Commented:
Hi Daniel

au is for Australia not Austria...

I repeat what I want to do...

I am fetching the html content of some page it contain many URLS some of those URLS start like so

http://blah.uni.edu.au/page/cgi-bin/extinfo.cgi?type=1&host=blah 

I want the above URLS to be replace by

http://new.url.com/blah

Then I want to display the page again This page should be exatly as the page I extrat the information from except the links on some of the URLS that started with http://blah.uni.edu.au/page/cgi-bin/extinfo.cgi?type=1&host=blah should have been replace with http://new.url.com/blah


I have increased the points to 100, i thought it was an easy and straight forwar question...

Thanx again



0
 
baz_can_fix_itAuthor Commented:
Hi Again

I think i know where my problem is...Its the way i am fteching.... Batalf technique works if I use a plain text... So I'll split the points...and I'll give more points to who ever answers this...

I am going to use Curl to fetch like so

$ch = curl_init ("http://username:password@somesite.com.au/cgi-bin/problems.cgi");
if (!$ch) {
die ("Cannot initialize a new cURL handle\n");
}

$data = curl_exec ($ch);
// Data is returned on success, error code on failure
if (is_int ($data)) {
die ("cURL error: " . curl_error ($ch) . "\n");
}

//print ($data);

curl_close ($ch);

when I look at $data it contains 1... how do I use "str_replace" in the above code... I mean what variable contains the buffer?

Cheers
0
 
baz_can_fix_itAuthor Commented:
PS I found this on a site...thats why i know the problem was fopen...

fopen() will not work (PHP <= 4.3.3) getting data from a URL + QUERY_STRING, like:
http://www.example.com/example.php?IdCategory=4
0
 
baz_can_fix_itAuthor Commented:
Dont worry all problem solved....

<?php
$string = "http://blah.uni.edu.au/page/cgi-bin/extinfo.cgi?type=1&host=blah";
$new="http://new.url.com/blah";
$url = "blah.uni.edu.au/page/cgi-bin/status.cgi";
$ch = curl_init();    
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  
curl_setopt($ch, CURLOPT_URL, $url);  
curl_setopt($ch, CURLOPT_USERPWD, "myusername:mypassword");  
$result = curl_exec($ch);  
curl_close($ch);  
echo str_replace($string,$new,$result);
?>

Thanx all

Cheers
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 6
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now