TextReplace.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. <?php
  2. namespace app\model;
  3. use think\Model;
  4. class TextReplace extends Model
  5. {
  6. protected $connection = 'mysql';
  7. protected $pk = 'id';
  8. protected $name = 'text_replace';
  9. public static function setreplace($textarry)
  10. {
  11. if (!empty($textarry)) {
  12. $isarray = 0;
  13. if (is_array($textarry)) {
  14. $isarray = 1;
  15. $textarry = json_encode($textarry,JSON_UNESCAPED_UNICODE);
  16. }
  17. $textReplace = self::where('status', 1)->where('weid', weid())->cache(180)->order('sort asc')->select()->toArray();
  18. if (!empty($textReplace)) {
  19. foreach ($textReplace as $vo) {
  20. if ($isarray) {
  21. $textarry = str_replace(str_replace('/','\/',$vo['sourcetext']), str_replace('/','\/',$vo['replacetext']), $textarry);
  22. } else {
  23. $textarry = str_replace($vo['sourcetext'], $vo['replacetext'], $textarry);
  24. }
  25. }
  26. }
  27. if ($isarray) {
  28. $textarry = json_decode($textarry, true);
  29. }
  30. }
  31. return $textarry;
  32. }
  33. }