使用 preg_replace 进行格式化

发布于 2024-08-10 17:24:50 字数 676 浏览 6 评论 0原文

图案和更换有困难。如何使替换回显最终产品,例如

INSERT INTO `table` (`person`, `file`) VALUES
('test','test'),
('test2','test2'),
('test3','test3');

我正在尝试将字符串插入 SQL,但我需要格式化下面的当前字符串才能执行此操作,并且我需要字符串的最后一部分 test3: test3 或任何关闭 SQL 模式的文本 ('test3','test3');

<?php
$string = 'test:test test2:test2 test3:test3';
$pattern = '';
$replacement = '';
echo preg_replace($pattern, $replacement, $string);
?>

另外,它也可以有这样的字符串吗? '[电子邮件受保护]:test test2:test2' 而电子邮件始终位于冒号之前。

Having trouble with pattern and replacement. How can I make the replacement echo a final product such as

INSERT INTO `table` (`person`, `file`) VALUES
('test','test'),
('test2','test2'),
('test3','test3');

I am trying to insert the string into SQL but I need to format the current string below to do so, and I need to have the last part of the string test3:test3 or whatever the text may be to close the SQL pattern ('test3','test3');

<?php
$string = 'test:test test2:test2 test3:test3';
$pattern = '';
$replacement = '';
echo preg_replace($pattern, $replacement, $string);
?>

Also, can it also have a string such as this? '[email protected]:test test2:test2' whereas the email will be before the colon at ALL times.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不甘平庸 2024-08-17 17:24:50

尝试这样的事情:

$string = 'test:test test2:test2 test3:test3';
$patterns = array("/([^\s:]+):([^\s:]+)/", "/\s++\(/");
$replacements = array("('$1', '$2')", ", (");
$sql = 'INSERT INTO `table` (`person`, `file`) VALUES ' . preg_replace($patterns, $replacements, $string) . ';';
echo $sql . "\n";

解释:

Regex 1
  ([^\s:]+)   # match one or more chars other than white space chars and colons and store it in group 1
  :           # match a colon 
  ([^\s:]+)   # match one or more chars other than white space chars and colons and store it in group 2 
Replacement 1
  (           # insert a '('
  '$1'        # insert what is matched in group 1 and surround it with single quotes
  ,           # insert ', '
  '$2'        # insert what is matched in group 2 and surround it with single quotes
  )           # insert a ')'

Regex 2
  \s++        # match one or more white space chars
  \(          # match a '('
Replacement 2
  , (         # insert ', ('

Try something like this:

$string = 'test:test test2:test2 test3:test3';
$patterns = array("/([^\s:]+):([^\s:]+)/", "/\s++\(/");
$replacements = array("('$1', '$2')", ", (");
$sql = 'INSERT INTO `table` (`person`, `file`) VALUES ' . preg_replace($patterns, $replacements, $string) . ';';
echo $sql . "\n";

An explanation:

Regex 1
  ([^\s:]+)   # match one or more chars other than white space chars and colons and store it in group 1
  :           # match a colon 
  ([^\s:]+)   # match one or more chars other than white space chars and colons and store it in group 2 
Replacement 1
  (           # insert a '('
  '$1'        # insert what is matched in group 1 and surround it with single quotes
  ,           # insert ', '
  '$2'        # insert what is matched in group 2 and surround it with single quotes
  )           # insert a ')'

Regex 2
  \s++        # match one or more white space chars
  \(          # match a '('
Replacement 2
  , (         # insert ', ('
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文