【问题标题】:Find textarea and replace with something else查找 textarea 并替换为其他内容
【发布时间】:2011-12-16 12:43:02
【问题描述】:
<html>
<head><title>Example page</title></head>
<body>
<textarea name="description">This is some text</textarea>
<input type='text' name='bla' value='a textinput' />
<textarea name="interests">What i like to do</textarea>
</body>
</html>

上面的html代码是一个变量的值。我想抓住所有的文本区域并用一些东西替换它们。从字符串中获取以下数组的方法是什么:

1: <textarea name="description">This is some text</textarea>
2: <textarea name="interests">What i like to do</textarea> 

【问题讨论】:

    标签: php regex


    【解决方案1】:
    1. 使用DOMDocument-&gt;loadHTML() 将 HTML 加载为 DOM 树,
    2. 查找textarea 元素(及其文本节点)
    3. 替换为相关元素。

    参照。 Robust, Mature HTML Parser for PHP


    本质上:避免此类任务的正则表达式。如果您坚持(并且保证 HTML 与指示的一样简单),请尝试:

    preg_replace('/<textarea name="([^"]+)">([^<]+)<\/textarea>/', '<input type="text" name="\\1" value="\\2">', $yourHtmlString);
    

    (demo)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-02
      • 2014-06-07
      • 2018-07-05
      • 1970-01-01
      • 2018-09-05
      • 1970-01-01
      相关资源
      最近更新 更多