Solved

Regular Expression str_replace, prg_match or preg_match_all

Posted on 2004-05-02
10
322 Views
Last Modified: 2012-06-21
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
Comment
Question by:baz_can_fix_it
  • 6
  • 3
10 Comments
 
LVL 32

Accepted Solution

by:
Batalf earned 60 total points
ID: 10975216
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
 
LVL 1

Assisted Solution

by:Daniel_Hegener
Daniel_Hegener earned 40 total points
ID: 10977973
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
 
LVL 1

Author Comment

by:baz_can_fix_it
ID: 10982433
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
 
LVL 1

Author Comment

by:baz_can_fix_it
ID: 10982440
hehehe Site not Sight...excuse the spellings...

cheers
0
 
LVL 1

Expert Comment

by:Daniel_Hegener
ID: 10984111
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 1

Expert Comment

by:Daniel_Hegener
ID: 10984127
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
 
LVL 1

Author Comment

by:baz_can_fix_it
ID: 11011185
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
 
LVL 1

Author Comment

by:baz_can_fix_it
ID: 11011653
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
 
LVL 1

Author Comment

by:baz_can_fix_it
ID: 11011747
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
 
LVL 1

Author Comment

by:baz_can_fix_it
ID: 11011769
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now