[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Regular Expression str_replace, prg_match or preg_match_all

Posted on 2004-05-02
10
Medium Priority
?
341 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
10 Comments
 
LVL 32

Accepted Solution

by:
Batalf earned 240 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 160 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

649 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