0

I would like to update specific strings in a table, but the strings themselves have variables. So,

https://www.youtube.com/watch?v=examplevideo

"examplevideo" is a variable string that I would like to preserve for the replacement string in

<iframe width="420" height="315" src="https://www.youtube.com/embed/examplevideo"></iframe>

Is there a way to do this in one update statement?

So, the table looks like this;

 select messageid,userid, subject, message  from feed_messages;
+-----------+--------+-------------+--------------------------------------------------------------------+
| messageid | userid | subject     | message                                                            |
+-----------+--------+-------------+--------------------------------------------------------------------+
|        74 |     67 | Test        | message                                                            |
|        75 |     67 | Test        | message                                                            |
|        77 |     67 | Test        | message                                                            |
|        78 |     67 | Test        | message                                                            |
|        90 |     70 | How are ya? | message                                                            |
|       106 |     67 | Test        | message                                                            |
|       107 |     67 | Test        | message                                                            |
|       117 |     67 | Test post   | Here's a test post;

https://www.youtube.com/watch?v=GWqD7GyJBVM |
+-----------+--------+-------------+--------------------------------------------------------------------+

I would like to keep everything in the message field for messageid 117 but modify the link so that it the video will be embeded when it posts.

4

2 回答 2

1

不幸的是,MySQL 的本机REGEXP表达式,即使与 结合使用REPLACE(),也不足以单独完成您想要的。

在纯 MySQL 中,这并不容易做到,除非您为 MySQL 使用这组用户定义函数 (UDF),它们为 MySQL 语句实现高级正则表达式功能,例如组捕获(这是您所需要的)。

如果您能够安装这些 UID,则可以使用以下内容:

UPDATE `feed_messages` SET
  `message` = REGEXP_REPLACE(
    `message`,
    'https?://www\.youtube\.com/watch\?v=([a-zA-Z0-9-_]{11})[a-zA-Z0-9-_%&=]*',
    '<iframe src="https://www.youtube.com/embed/$1"></iframe>')
WHERE
  `message` REGEXP 'youtube\.com/watch\?v=[a-zA-Z0-9-_]{11}';

如果您无法使用这些 UDF,那么这几乎必须在数据库之外完成。例如,在 PHP 中:

$query = 'SELECT `messageid`,`userid`,`subject`,`message` FROM `feed_messages`';
$msgs = $mysqli->query($query);
while ($row = $msgs->fetch_assoc()) {
  echo preg_replace(
    $row['message'],
    '/https?://www\.youtube\.com/watch\?v=([a-zA-Z0-9-_]{11})[a-zA-Z0-9-_%&=]*/im',
    '<iframe width="420" height="315" src="https://www.youtube.com/embed/$1"></iframe>')
}

希望这可以帮助!

于 2013-09-22T20:59:42.317 回答
0

您可以使用 SUBSTRING_INDEX 来确定字符串“ https://www.youtube.com/watch?v= ”的位置,然后再次执行此操作以获得下一个空格的位置。通过这两个位置,您可以提取视频 ID 并结合 Leonid 的替换和 CONCAT 来插入开始和结束 iframe 标记。

于 2013-09-22T20:35:17.593 回答