[英]Regex search and replace on all rows in a table with Laravel 4's Fluent or Eloquent
What's the best way to use Fluent or Eloquent to search for a pattern in a MySQL database and replace it? 使用Fluent或Eloquent在MySQL数据库中搜索模式并替换它的最佳方法是什么?
I've seen in the Laravel forums that you should be able to do a regex selection like so 我在Laravel论坛上看到你应该能够像这样进行正则表达式选择
// Searching for the word "mypattern"...
Page::where('words', 'regexp', DB::raw('/\bmypattern\b/'))->get();
But I've tried this method and it doesn't seem to select the rows I'm after. 但我已经尝试过这种方法,它似乎没有选择我追求的行。 In any case, actually don't want to JUST select-- I'd like to actually do a search and replace with regex .
在任何情况下,实际上不想只是选择 - 我想实际进行搜索并用正则表达式替换 。
What's the best way to do this with the options provided by Laravel? 使用Laravel提供的选项,最好的方法是什么?
I don't believe that 'regexp' is a valid query operator in Laravel 4. 我不相信'regexp'是Laravel 4中的有效查询运算符 。
It seems that Taylor has actually made a comment specific to this issue on Github under issue #838 , and he suggests using whereRaw instead of where. 似乎泰勒实际上在问题#838上对Github上的这个问题进行了专门评论,他建议使用whereRaw而不是where。
Here is an SO question regarding MySQL Regex Replace, which may help you to implement this. 这是关于MySQL Regex Replace的SO问题,它可以帮助您实现这一点。
You could possibly use a MySQL User Defined Function
(UDF) for what you're asking. 您可以使用MySQL
User Defined Function
(UDF)来处理您的问题。 Note that using a UDF
is not always compatible but might be good enough to do for your case. 请注意 ,使用
UDF
并不总是兼容,但可能足以满足您的需求。
The UDF package mod will implement your regular expression as a User Defined Function and is supported by the regular expression REGEXP
operator. UDF包mod将您的正则表达式实现为用户定义函数,并由正则表达式
REGEXP
运算符支持。
See MySQL regular expression functions 请参阅MySQL正则表达式函数
Another option would be to dump your data and do a find and replace. 另一种选择是转储数据并进行查找和替换。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.