{"id":27198528,"url":"https://github.com/andydune/stringreplace","last_synced_at":"2025-04-09T20:42:36.451Z","repository":{"id":62485913,"uuid":"131164157","full_name":"AndyDune/StringReplace","owner":"AndyDune","description":"Replace in given string meta data with real data.","archived":false,"fork":false,"pushed_at":"2025-02-27T10:07:41.000Z","size":42,"stargazers_count":5,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-23T22:37:57.975Z","etag":null,"topics":["php","string-replace"],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/AndyDune.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-04-26T13:59:58.000Z","updated_at":"2025-02-27T10:47:56.000Z","dependencies_parsed_at":"2022-11-02T10:15:28.128Z","dependency_job_id":null,"html_url":"https://github.com/AndyDune/StringReplace","commit_stats":null,"previous_names":[],"tags_count":17,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AndyDune%2FStringReplace","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AndyDune%2FStringReplace/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AndyDune%2FStringReplace/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AndyDune%2FStringReplace/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/AndyDune","download_url":"https://codeload.github.com/AndyDune/StringReplace/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248110130,"owners_count":21049442,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["php","string-replace"],"created_at":"2025-04-09T20:42:35.508Z","updated_at":"2025-04-09T20:42:36.442Z","avatar_url":"https://github.com/AndyDune.png","language":"PHP","readme":"# StringReplace\n\n[![Build Status](https://travis-ci.org/AndyDune/StringReplace.svg?branch=master)](https://travis-ci.org/AndyDune/StringReplace)\n[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE)\n[![Packagist Version](https://img.shields.io/packagist/v/andydune/string-replace.svg?style=flat-square)](https://packagist.org/packages/andydune/string-replace)\n[![Total Downloads](https://img.shields.io/packagist/dt/andydune/string-replace.svg?style=flat-square)](https://packagist.org/packages/andydune/string-replace)\n\n\nIt replace in given string meta data with real data.\n\nRequirements\n------------\n\nPHP version \u003e= 7.2\n\nInstallation\n------------\n\nInstallation using composer:\n\n```\ncomposer require andydune/string-replace\n```\nOr if composer was not installed globally:\n```\nphp composer.phar require andydune/string-replace\n```\nOr edit your `composer.json`:\n```\n\"require\" : {\n     \"andydune/string-replace\": \"^1\"\n}\n\n```\nAnd execute command:\n```\nphp composer.phar update\n```\n\n\n## SimpleReplace\n\nIt's very simple and lightweight replace methods. It uses `str_replace` function.\n\n```php\nuse AndyDune\\StringReplace\\SimpleReplace;\n\n$instance = new SimpleReplace();\n$instance-\u003eone = 'one_ok';\n$instance-\u003etwo = 'two_ok';\n\n$string = 'Gogoriki go #one# and #two#';\n$instance-\u003ereplace($string); // equals to 'Gogoriki go one_ok and two_ok' \n\n```\n\nThere is no any logic in it and it will no replace statements if no data to replace.\n\n## PowerReplace\n\nIt powerful replace class with string analytics with regular. \nThere are many functions built-in lib and you may add custom easily.\n\n### No case sensitive\n```php\nuse AndyDune\\StringReplace\\PowerReplace;\n\n$instance = new PowerReplace();\n$instance-\u003eone = 'one_ok';\n$instance-\u003eTWO = 'two_ok'; // upper key\n\n$string = 'Gogoriki go #ONE# and #two#';\n$instance-\u003ereplace($string); // equals to 'Gogoriki go one_ok and two_ok' \n\n```\n\n## Functions\n\nFunctions are described next to marker after `:` (you can change separator). \n\nFunctions can get parameters: `#CODE:maxlen(10)#` or `#CODE:maxlen(\"10\")#` \n\nSymbols: __:__ __(__ __)__ __,__ __\"__ __'__ are reserved to use as parameters for function. \nSo if you want to use it you mast encase it with quotes (or single quotes).\n\n\nThis is correct usage:\n```php\n$string = \"Params: #weight:prefix(\\\"'\\\"):postfix('\"')#\";\n$string = \"Params: #weight:prefix(\\\":\\\"):postfix(':')#\";\n$string = \"Params: #weight:prefix(\\\"(\\\"):postfix(')')#\";\n$string = \"Params: #weight:prefix(\\\", \\\"):postfix(', ')#\";\n```\n\nMore then one function : `#CODE:maxlen(10):escape#`\n\n### escape\n\nApply `htmlspecialchars` with inserted value.\n\n```php\nuse AndyDune\\StringReplace\\PowerReplace;\n\n$string = 'Gogoriki go #ONE:escape#';\n$instance = new PowerReplace();\n$instance-\u003eone = '\u003cb\u003eone_ok\u003c/b\u003e';\n$instance-\u003ereplace($string);  // equals to 'Gogoriki go \u0026lt;b\u0026gt;one_ok\u0026lt;/b\u0026gt;'\n```\n\n### addcomma\n\nIt adds comma before inserted value if it is not empty.\n\n```php\nuse AndyDune\\StringReplace\\PowerReplace;\n\n$string = 'Gogoriki go #one##two:comma#';\n$instance = new PowerReplace();\n$instance-\u003eone = 'swim';\n$instance-\u003eone = 'play';\n$instance-\u003ereplace($string);  // equals to 'Gogoriki go swim, play'\n\n\n$string = 'Gogoriki go #one##two:comma#';\n$instance = new PowerReplace();\n$instance-\u003eone = 'swim';\n$instance-\u003ereplace($string);  // equals to 'Gogoriki go swim\n```\n\n`comma` function may get params: `comma(param1, param2)`\n\n- *param1* set to `1` if you want to miss first comma appearance in string\n- *param2* set to `1` if you want to begin new group of words for next missing  of first comma appearance in string\n\n```php\n$string = 'I know words: #it:addcomma(1)##and_it:addcomma(1)# and #and_it_2:addcomma(1, 1)#';\n$instance = new PowerReplace();\n$instance-\u003esetArray([ \n    'it' =\u003e 'eat',\n    'and_it' = 'play',\n    'and_it_2' = 'sleep'\n    ]);\n$instance-\u003ereplace($string);  // equals to 'I know words: eat, play and sleep'\n\n```\n\n### maxlen\n\nReplace marker with value if string behind this one is less then poined in parameter.\n\n```php\nuse AndyDune\\StringReplace\\PowerReplace;\n\n$string = 'Gogoriki go #one##two:masxlen(5):addcomma#';\n$instance = new PowerReplace();\n\n$instance-\u003eone = 'swim';\n$instance-\u003eone = 'play';\n$instance-\u003ereplace($string);  // equals to 'Gogoriki go swim, play'\n\n$instance-\u003eone = 'swim';\n$instance-\u003eone = 'play games';\n$instance-\u003ereplace($string);  // equals to 'Gogoriki go swim'\n```\n\n### printf\n\nPrint formatted string if it is not empty.\n\n```php\n$string = 'I know words: #it:printf(«%s»):addcomma(1)##and_it:printf(«%s»):addcomma(1)# and #and_it_2:printf(«%s»):addcomma(1, 1)#';\n$instance = new PowerReplace();\n$instance-\u003eit = 'eat';\n$instance-\u003eand_it_2 = 'sleep';\n$instance-\u003ereplace($string); // equals to  I know words: «eat» and «sleep»\n```\n\n### plural\n\nPluralize the title for number.\n```php\n$string = 'I see #count# #count:plural(man, men)#';\n$instance = new PowerReplace();\n$instance-\u003ecount = 1;\n$instance-\u003ereplace($string); // I see 1 man\n$instance-\u003ecount = 21;\n$instance-\u003ereplace($string); // I see 21 men\n```\n\n\n### pluralrus\n\nRussian pluralize the title for number.\n```php\n$string = 'У меня есть #count# #count:pluralrus(яблоко, яблока, яблок)#';\n$instance = new PowerReplace();\n\n$instance-\u003ecount = 1;\n$instance-\u003ereplace($string)); // У меня есть 1 яблоко\n\n$instance-\u003ecount = 21;\n$instance-\u003ereplace($string); // У меня есть 21 яблоко\n\n$instance-\u003ecount = 2;\n$instance-\u003ereplace($string); // У меня есть 2 яблока\n\n$instance-\u003ecount = 5;\n$instance-\u003ereplace($string); // У меня есть 5 яблок\n```\n\n### prefix\n\nIt shows given string as prefix only if value behind the key is not empty.\n```php\n$string = 'Vegetables I have: #apple_count:prefix(\"apples \"):addcomma(1)##orange_count:prefix(\"oranges \"):addcomma(1)#';\n$instance = new PowerReplace();\n$instance-\u003eapple_count = 1;\n$instance-\u003ereplace($string); // Vegetables I have: apples 1\n```\n\n### postfix\n\nIt shows given string as postfix only if value behind the key is not empty.\n```php\n$string = 'Params: #weight:prefix(\"weight: \"):postfix(kg)##growth:prefix(\"growth: \"):postfix(sm):addcomma#';\n$instance = new PowerReplace();\n$instance-\u003eweight = 80;\n$instance-\u003egrowth = 180;\n$instance-\u003ereplace($string); // Params: weight: 80kg, growth: 180sm\n\n```\n\n### showIfEqual\n\nIt shows string given in second param if first param is equal to value behind the placeholder.\n```php\n$string = 'Anton #weight:showIfEqual(80, \"has normal weight\")##weight:showIfEqual(180, \"has obesity\")#.';\n$instance = new PowerReplace();\n$instance-\u003eweight = 80;\n$instance-\u003ereplace($string); // Anton has normal weight.\n\n$string = 'Anton #weight:showIfEqual(80, \"has normal weight\")##weight:showIfEqual(180, \"has obesity\")#.';\n$instance = new PowerReplace();\n$instance-\u003eweight = 180;\n$instance-\u003ereplace($string); // Anton has obesity.\n```\n\n\n### showIfOtherValueNotEmpty\n\nIt shows string value behind the current placeholder if another is not empty.\n\n```php\n$string = 'Variants #type[name]:showIfOtherNotEmpty(type[value])##type[value]:prefix(\": \")#';\n$instance = new PowerReplace();\n$instance-\u003esetArray(['type'=\u003e ['name' =\u003e 'color', 'value' =\u003e 'green']]);\n$instance-\u003ereplace($string); // Variants color: green\n```\n\n## Custom Functions\n\nYou can add your own functions with replace rules. Markers and functions are not case sensitive.\n\n```php\n\n$string = 'Where is #word:leftAndRight(_)#?';\n// or the same\n$string = 'Where is #WORD:LEFTANDRIGHT(_)#?';\n\n$functionHolder = new FunctionsHolder();\n\n// add custom function with name leftAndRight\n$functionHolder-\u003eaddFunction('leftAndRight', function ($string, $symbol = '') {\n    return $symbol . $string . $symbol;\n});\n$instance = new PowerReplace($functionHolder);\n$instance-\u003eword = 'center';\n$instance-\u003ereplace($string); // Where is _center_?\n\n```\n## Application\n\n- [HtmlTable](https://github.com/AndyDune/HtmlTable/blob/master/src/Builder.php)\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fandydune%2Fstringreplace","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fandydune%2Fstringreplace","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fandydune%2Fstringreplace/lists"}