/    Sign up×
Community /Pin to ProfileBookmark

Replace string inside a string included in an external txt file.

How can I use the str_replace function so that the string it looks into to replace things from is included in an external file.

This external file changed automatically out of my control so I can’t just copy & paste the string.

to post a comment
PHP

10 Comments(s)

Copy linkTweet thisAlerts:
@JonaOct 15.2004 — [font=trebuchet ms]Assuming you have the url_fwrappers directive set to "on" in your PHP.ini file...[/font]

[code=php]
<?php
if(@$fh = fopen('http://www.site.com/dir/file.txt', 'r')){
@$str = fread ($fh, filesize('http://www.site.com/dir/file.txt'));
$str = str_replace("something", "something else", $str);
@fclose($fh);
} else {
die ("Error opening file in ".__FILE__." on line ".__LINE.".");
}

echo $str;
?>
[/code]
Copy linkTweet thisAlerts:
@stevoradioauthorOct 15.2004 — Where can I find the php.ini ??

and what does editing the url_fwrappers do?
Copy linkTweet thisAlerts:
@JonaOct 15.2004 — [font=trebuchet ms]It allows you to use the filesystem functions to open a remote file. If you don't have access to your server's php.ini file, add the following line to the first part of the script I gave you. Depending on your PHP version, though, this may already be set. (If you're running PHP 4.8.3 or greater, you can probably just use [url=http://php.net//file_get_contents]file_get_contents[/url] or the fopen function; the directive defaulted to allow instead of disallow in PHP 4.8.3.)[/font]

[code=php]
if(ini_get("allow_url_fopen") == "0") ini_set("allow_url_fopen", "1");
[/code]
Copy linkTweet thisAlerts:
@stevoradioauthorOct 15.2004 — I have created this
[code=php]

<script language="php">
if(ini_get("allow_url_fopen") == "0"){
ini_set("allow_url_fopen", "1");
}

if(@$fh = fopen('http://www.cmpradio.co.uk/livetext.txt', 'r')){
@$str = fread ($fh, filesize('http://www.cmpradio.co.uk/livetext.txt'));
$str = str_replace("+", " ", $str);
@fclose($fh);
} else {
die ("Error opening file in ".__FILE__." on line ".__LINE.".");
}

echo $str;

</script>

[/code]


It displays nothing - what's wrong with it?
Copy linkTweet thisAlerts:
@JonaOct 15.2004 — [font=trebuchet ms]Tell me if you get an error with the following.[/font]

[code=php]
<script language="php">
if(ini_get("allow_url_fopen") == "0"){
ini_set("allow_url_fopen", "1");
}

$fh = fopen('http://www.cmpradio.co.uk/livetext.txt', 'r');
@$str = fread ($fh, filesize('http://www.cmpradio.co.uk/livetext.txt'));
$str = str_replace("+", " ", $str);
@fclose($fh);

echo $str;
</script>
[/code]
Copy linkTweet thisAlerts:
@stevoradioauthorOct 15.2004 — still blank...
Copy linkTweet thisAlerts:
@JonaOct 15.2004 — [font=trebuchet ms]You must have error reporting turned off in your configuration file as well, for some reason. Try the following, and tell me if you get errors, and if so what they are:[/font]

[code=php]
<script language="php">
if(ini_get("allow_url_fopen") == "0"){
ini_set("allow_url_fopen", "1");
}
error_reporting(E_STRICT | E_ALL);

$fh = fopen('http://www.cmpradio.co.uk/livetext.txt', 'r');
$str = fread ($fh, filesize('http://www.cmpradio.co.uk/livetext.txt'));
$str = str_replace("+", " ", $str);
fclose($fh);

echo $str;
</script>
[/code]
Copy linkTweet thisAlerts:
@JonaOct 15.2004 — [font=trebuchet ms]I've pinpointed the error; sorry to cause you so much trouble. I forgot that the filesize stat function doesn't work remotely, so you have to do it until the file has been completely read. Use the following instead.[/font]

[code=php]
if(ini_get("allow_url_fopen") == "0"){
ini_set("allow_url_fopen", "1");
}

$str = "";

if($fh = fopen('http://www.cmpradio.co.uk/livetext.txt', 'r')){
while(!feof($fh)){
$str .= fgets ($fh);
}
$str = str_replace("+", " ", $str);
fclose($fh);
} else {
die ("Error opening file in ".__FILE__." on line ".__LINE.".");
}

echo $str;
[/code]
Copy linkTweet thisAlerts:
@stevoradioauthorOct 15.2004 — Great mate, works great. Just a starting point what i want it to do. Thanks for the starter ?
Copy linkTweet thisAlerts:
@JonaOct 15.2004 — [font=trebuchet ms]Happy to help. ? [/font]
×

Success!

Help @stevoradio spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.8,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...