Solved

Regular Expression str_replace, prg_match or preg_match_all

Posted on 2004-05-02
10
327 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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
 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

860 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