Solved

Regular Expression str_replace, prg_match or preg_match_all

Posted on 2004-05-02
10
330 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 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
Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

 
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

Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
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 …

717 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