PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, February 3, 2022

[FIXED] How to search and replace the next value after specific text in a file - PHP

 February 03, 2022     cakephp, php     No comments   

Issue

I want to search and replace the string from a PO file (its a translation file for CakePHP) after the specific character which is #id:

This are the sample text inside a PO file.

#id: 4
msgctxt "team 1"
msgid "john"
msgstr "전세계 강사와 영어회화 레슨을 받을 수 있는 서비스입니다."

#id: 5
msgctxt "team 2"
msgid "jane"
msgstr "translation here"

#id: 6
msgctxt "team 3"
msgid "jen"
msgstr "translation here"


..
and so on..

Example. I want to replace the strings after #id: 5, which has msgctxt, msgid, msgstr, from my database.

I got this column in my database msgid, msgctxt and msgstr

The expected output will be like this:

#id: 4
msgctxt "team 1"
msgid "john"
msgstr "전세계 강사와 영어회화 레슨을 받을 수 있는 서비스입니다."

#id: 5
msgctxt "team 2"
msgid "jane"
msgstr "translation here"

#id: 6
msgctxt "team 3"
msgid "jen"
msgstr "translation here"


..
and so on..

I have tried this one, but I don't know how to find the strings using str_replace inside the PO file.

$newContent .= '#id: ' . $po['id'] . "\n";
$newContent .= 'msgctxt "' . trim($po['msgctxt']) . '"' . "\n";
$newContent .= 'msgid "' . trim($po['msgid']) . '"' . "\n";
$newContent .= 'msgstr "'  . trim($po['msgstr']) . '"';


$file = file_get_contents($filepath);
$data = str_replace($file, $newContent, $file); // I dont think if its correct to find the string.
file_put_contents($filepath, $data);

PLEASE NOTE: That, #id: is dynamic.

UPDATE: Sometimes, I got this data inside the PO file.

#id: 4
#: https://url // sometimes don't have this
msgctxt "team 1" // sometimes don't have this
msgid "john"
msgstr "translation here"

#id: 5
msgid "jane"
msgstr "translation here"

#id: 6
msgctxt "team 3"
msgid "jen"
msgstr "translation here"

Solution

Well, I think you can try this:

$file = <<<str
#id: 4
msgctxt "team 1"
msgid "john"
msgstr "전세계 강사와 영어회화 레슨을 받을 수 있는 서비스입니다."

#id: 5
msgctxt "team 2"
msgid "jane"
msgstr "translation here"

#id: 6
msgctxt "team 3"
msgid "jen"
msgstr "translation here"


..
and so on..
str;

$newContent = <<<str
#id: $1
msgctxt "$2"
msgid "$3"
msgstr "$4"
str;

$data = preg_replace('/#id:\s*([0-9]+)\s*\nmsgctxt\s*"(.*?)"\s*\nmsgid\s*"(.*?)"\s*\nmsgstr\s*"(.*?)"/', $newContent, $file);

$1, $2 and $3 refer to the origin content behind msgctxt, msgid and msgstr, just change it to any information you like. Also you can change [0-9]+ into any specific id you want.



Answered By - Neptune
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing