0

let say i have a text :

this line is the first line of this text called %title%

this line is the second one

the third line, %title% shouldn't be replaced

...

last line

now I want to use PHP so the text becomes :

this line is the first line of this text called MY_TITLE

this line is the second one

the third line, %title% shouldn't be replaced

...

last line

NOTICE the %title% on the third line also

what would be the best (fastest) way to do that ?

Community
  • 1
  • 1
vdegenne
  • 12,272
  • 14
  • 80
  • 106

3 Answers3

4

You can load only first line to variable, than do str_ireplace and then put first line + rest of the file back together.

$data = explode("\n", $string);
$data[0] = str_ireplace("%title%", "TITLE", $data[0]);    
$string = implode("\n", $data);

Its not the most efficient way imho, but suitable and fast to code.

Martin Perry
  • 9,232
  • 8
  • 46
  • 114
  • thanks for this solution, however if the file is really big, i guess this exploding and imploding will cost a lot of memory + processing usage – vdegenne Jun 04 '13 at 13:21
  • @Whistletoe no it does not, i will explode all the line of the file to return an array in $data – vdegenne Jun 04 '13 at 13:27
4

There are two approaches:

  • If you are sure, that the replacement has to be done exactly one time (i.e. the placeholder will allways be in the first line, and allways only onec), you can use $result=str_replace('%title%','MY_TITLE',$input,1)

  • If this is not guaranteed, you need to separate the first line:

.

$pos=strpos($input,"\n");
if (!$pos) $result=$input;
else $result=str_replace('%title%','MY_TITLE',substr($input,0,$pos)).substr($input,$pos);
Eugen Rieck
  • 64,175
  • 10
  • 70
  • 92
  • I like this solution, do you think the substr's will be harmful for performance if the program involves the use of large files ? – vdegenne Jun 04 '13 at 13:41
  • **Every** such String manipulation in PHP involves copying the string, which is of memory and CPU concern, if the string is big. The solutions I presented are those, that IMHO involve the **least** of it. If you read the string from a file, please update your answer and it might be much easier (think `fopen(); fgets(); str_replace(); fread(); fclose()`) – Eugen Rieck Jun 04 '13 at 13:46
  • actually the last number in str_replace is just a counter set to the number of replacement performed in the string.. – vdegenne Jun 04 '13 at 14:01
  • @Oddant: Exactly. So **IF** you are sure, you have the placeholder exactly once in the first line, **THEN** processing the first line is equal to procesing the first occurrence. – Eugen Rieck Jun 04 '13 at 14:05
  • No I mean the last argument is a reference to a variable, once the function is done, the variable contains the number of replacements performed through all the text. it can't work. – vdegenne Jun 04 '13 at 14:08
3

You can use preg_replace() it's just one line of code ;)

$str = "this line is the first line of this text called %title%\n
this line is the second one\n
the third line, %title% shouldn't be replaced\n
last line";

echo preg_replace('/%title%$/m','MY_TITLE',$str);

Explanation of regex:

  • /%title% means %title%
  • $ means end of line
  • m makes the beginning of input (^) and end of input ($) codes also catch beginning and end of line respectively

Output:

this line is the first line of this text called MY_TITLE
this line is the second one the third line, %title% shouldn't be replaced
last line
Robert
  • 19,800
  • 5
  • 55
  • 85
  • i'm sorry i should have noted that %title% can be anywhere in the first line – vdegenne Jun 04 '13 at 13:35
  • 1
    You want to remove it just from the first line? So why not to remove only 1 occurence with `$str = str_replace( "%title%", "MY_TITLE", $str, 1 );` – Robert Jun 04 '13 at 13:37