Ron Ashrovy
Ron Ashrovy

Reputation: 113

Replace some blank space in txt file using PHP

I'm newbie in PHP.

I want to replace some blank space in my txt.file with some word using PHP. This my .txt file (text.txt):

A Chatbot record

Hello, how are you?

I'm Fine

And this what I want

A chatbot record
<category>
<pattern>Hello, how are you?</pattern>
<template>I'm Fine</template>
</category>

I already try some code PHP, with str_replace("&nbsp;","<category><pattern>","\n") but it won't work, what should I do?

Here's failed my experiment:

<?php
$myfile = fopen("text.txt", "r+") or die("Unable to open file!");
str_replace("&nbsp;", "<category><pattern>", "\n"); //I know it's wrong in here, please help me to fix it!
echo fread($myfile,filesize("text.txt"));
fclose($myfile);
?>

Thank you, and sorry for my bad english

Upvotes: 1

Views: 1029

Answers (4)

apokryfos
apokryfos

Reputation: 40653

Regex is probably the way to go here.

<?php
$string = file_get_contents("text.txt");

$replaced = preg_replace("/A Chatbot record(\W+)([^\n]+)\W+([^\n]+)/mi", "A chatbot record\n<category>\n<pattern>$2</pattern>\n<template>$3</template>\n</category>", $string);
file_put_contents("text.txt", $replaced);

This can match all patterns

http://sandbox.onlinephpfunctions.com/code/95184665ec627cd9354d67853bb696865d51a03d

Upvotes: 1

Ivo P
Ivo P

Reputation: 1712

<?php
$string = 'A Chatbot record

Hello, how are you?

I\'m Fine';

$reg = '#^(.*\n)\n(.*)\n\n(.*)#';
$replace = '$1<category>
  <pattern>$2</pattern>
  <template>$3</template>';

echo preg_replace($reg, $replace, $string);

A regex can be more flexible than exploding or strpos solutions. (note: is still missing here.

question remains wether all texts are always on 1 line

Upvotes: 0

Julian Chan
Julian Chan

Reputation: 466

You should probably do this:

<?php
$myfile = fopen("text.txt", "r+") or die("Unable to open file!");
str_replace("&nbsp;", "<category><pattern>", $myfile);
echo fread($myfile,filesize("text.txt"));
fclose($myfile);
?>

If you still have issues, it is probably because your file doesn't contain   and perhaps you are looking for \n (new line character) instead.

<?php
$myfile = fopen("text.txt", "r+") or die("Unable to open file!");
str_replace("\n", "<category><pattern>", $myfile);
echo fread($myfile,filesize("text.txt"));
fclose($myfile);
?>

The problem was that you were not replacing the string in the file, rather you were replacing " " in the string "\n".

Upvotes: 0

Baptiste
Baptiste

Reputation: 1785

$string = 'A Chatbot record

Hello, how are you?

I\'m Fine';

$explode = preg_split("#\n\s*\n#Uis", $string);
echo $explode[0];
echo "<category>";
echo "<pattern>".$explode[1]."</pattern>";
echo "<template>".$explode[2]."</template>";
echo "</category>";

For me the best way is to split your String into an array so you can work more easily after. Here is the array thanks to preg_split :

array(3) {
  [0] =>
  string(16) "A Chatbot record"
  [1] =>
  string(19) "Hello, how are you?"
  [2] =>
  string(8) "I'm Fine"
}

Upvotes: 0

Related Questions