【问题标题】:PHP: String replace with RegexPHP:字符串替换为正则表达式
【发布时间】:2014-03-31 11:16:09
【问题描述】:

我试图让 Regex 为这个脚本工作。

我需要它做的是从插入的 youtube 代码中获取宽度和高度,并将其替换为指定的高度和宽度。

$youtube = str_replace('width="' * ' "','width="500"' $_POST['youtube']);

有人可以帮忙吗?

【问题讨论】:

  • 1) 显示 php 代码以显示 youtube,2) 显示您尝试过的内容
  • 我确实添加了代码,

标签: php regex str-replace


【解决方案1】:

假设你的代码是:

$code = 'width="100" and height="200"';

然后使用preg_replace()执行下面的正则表达式替换

$code = preg_replace('/width="\d+"/', 'width="XXX"', $code);
$code = preg_replace('/height="\d+"/', 'height="XXX"', $code);

\d+ 表示从长度一开始的任意位数。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-13
    • 1970-01-01
    • 2017-02-04
    • 1970-01-01
    相关资源
    最近更新 更多