/    Sign up×
Community /Pin to ProfileBookmark

randomly insert a character when writing to a text file

Hi all

I’m using this code to write to a text file:

[code=php]if (file_exists($file)) {

$fp = fopen($file, “w”) or die(
require(“fileerror.php”)
); }
$numBytes = fwrite($fp, $values) or die(
require(“fileerror.php”)
);

fclose($fp);[/code]

please tell me if this could be improved.

what I need to do is randomly insert a character when writing to a text file.

So it looks all jumbled up when loaded at.

e.g.

this is the text file now:

[CODE]
Email Notification: emails sent to you whenever someone replies. Only registered users are eligible.
[/CODE]

and this is how I’d like it:

[CODE]
Em%a%il No%%%ti%fi%%c%ati%%%on: e%a%i%ls s%en%t to% y%ou whe%%ne%ver %%som%eo%ne rep%%li%es%%%. On%ly %reg%%is%te%red%%% us%e%rs are e%%%lig%i%ble%.
[/CODE]

it doesn’t have to be the % character, it can be anything that will not mess with the php code that generates it.

Can this be done?

Cheers
Chris

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@NogDogSep 24.2004 — [i](Edited to fix substr() parameters)[/i]
[code=php]
$myString = "Email Notification: emails sent to you whenever " .
"someone replies. Only registered users are eligible.";
$newString = "";
for($i=0; $i<strlen($myString); $i++)
{
$newString .= substr($myString, $i, 1);
if(rand(1,10) <= 4) # 40% of the time
{
$newString .= '%'; # change this to desired character
}
}
echo "<p>$newString</p>n";
[/code]
Copy linkTweet thisAlerts:
@artemisSep 24.2004 — [font=technical][size=3]please note that this will not work.

this line will always insert the first charater.

[font=monospace][size=5]
[code=php]
$newString .= substr($myString,0,1);
[/code]
[/size][/font]

and should be changed to:

[font=monospace][size=5][code=php]
$newString .= substr($myString,$i,$i+1);
[/code]
[/size][/font]

[/size][/font]
Copy linkTweet thisAlerts:
@NogDogSep 24.2004 — [i]Originally posted by artemis [/i]

[B][font=technical][size=3]please note that this will not work.

this line will always insert the first charater.

[font=monospace][size=5]
[code=php]
$newString .= substr($myString,0,1);
[/code]
[/size][/font]

and should be changed to:

[font=monospace][size=5][code=php]
$newString .= substr($myString,$i,$i+1);
[/code]
[/size][/font]

[/size][/font] [/B][/QUOTE]

Oops. :eek:

Actually, it should be changed to:
[code=php]$newString .= substr($myString, $i, 1);[/code]
(I am now off to edit my original response....)
Copy linkTweet thisAlerts:
@artemisSep 24.2004 — lol damn, JavaScript on the brain!
Copy linkTweet thisAlerts:
@cybercampbellauthorSep 24.2004 — Fantastic...Thanks.

Chris ? ? ? ? ? ?
Copy linkTweet thisAlerts:
@cybercampbellauthorSep 24.2004 — OK... all is great.

I just need to be able to write 'ö' as the desired character.

if I tell it to write '&ouml;' it doesn't write the character to the text file. just the '&ouml/'.

can this be done?

Chris

arrrr! it won't show the code for &ouml;
Copy linkTweet thisAlerts:
@NogDogSep 24.2004 — [i]Originally posted by cybercampbell [/i]

[B]OK... all is great.



I just need to be able to write 'ö' as the desired character.



if I tell it to write '&ouml;' it doesn't write the character to the text file. just the '&ouml/'.



can this be done?



Chris



arrrr! it won't show the code for &ouml; [/B]
[/QUOTE]

Maybe use [url=http://us4.php.net/manual/en/function.html-entity-decode.php]html_entity_decode[/url]?
×

Success!

Help @cybercampbell 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 6.17,
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: @nearjob,
tipped: article
amount: 1000 SATS,

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

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...