1
<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>

The above html code is the value of a variable. I want to grab all the textareas and replace them by something. What would be the way to get following array from the string:

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

1 Answers1

2
  1. Use DOMDocument->loadHTML() to load the HTML as a DOM tree,
  2. Find the textarea elements (and their text nodes)
  3. Replace with relevant element.

Cf. Robust, Mature HTML Parser for PHP


In essence: avoid regular expressions for this kind of tasks. If you insist (and the HTML is guaranteed to be as simple as indicated), try:

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

(demo)

Community
  • 1
  • 1
jensgram
  • 31,109
  • 6
  • 81
  • 98