Get Textarea Value with Simpe HTML Dom

573 views Asked by At

i using simple_html_dom.php

how to get textarea value if the website has used bad tag.

the textarea tag already closed before </textarea> like input tag.

Textarea HTML like below:

<textarea name="xxx" id="xxx" />this is value</textarea>

When i use this function, i dont get anything

$textarea = $html->find("textarea[name=xxx]"); 
$contents = $textarea->innertext;
echo $contents;

how to get 'this is value' using simple_html_dom.php or other alternative?

Thank you

2

There are 2 answers

1
Enissay On

Well, my previous comment won't work in this case, I'll leave it for info though...

Another approach is to clean it up before parsing it with simple_html_dom using Tidy extension. But it seems not to be working here either...

A last approach I can think of, and if this is your only problematic case, is to use regex to get what you want:

  • Using <textarea.*?name="xxx".*?id="xxx".*?\/>([^<]+)<\/textarea> ==> RegEx DEMO
  • The output will be in group one of the resulting array $match. Check this working code:

    $input =  <<<_DATA_ 
    <textarea name="xxx" id="xxx" />this is value</textarea>
    _DATA_;
    
    $pattern = '/<textarea.*?name="xxx".*?id="xxx".*?\/>([^<]+)<\/textarea>/';
    
    preg_match($pattern, $input, $match);
    var_dump($match)
    

    Working DEMO

0
Karshan Khodbhaya On

It is easy to get the value of a Teaxtarea in javascript:

 <script type=text/javascript>

function getValueTextarea()
{
   var vl=document.getElementById("tx").value;
   alert(vl);
}

</script>

<body>
   <textarea id="tx">Value Of Textarea</textarea>
   <input id="button" value="Get Value" onclick="getValueTextarea()">  
</body>