Question

preg_match("/ [>](.*)[<] /", '<textarea width="500" >web scripting language of choice.</textarea>',$matches);
print_r ($matches);

i want to return just "web scripting language of choice." form this string. please help me. to arrive this PHP

Was it helpful?

Solution

Using a DOM parser

HTML is not a regular language and cannot be correctly parsed using a regular expression. Use a DOM parser instead. Here's how it can be done using PHP's DOMDocument class:

$html = <<<HTML
<textarea width="500" >web scripting language of choice.</textarea>
HTML;

$dom = new DOMDocument;
$dom->loadHTML($html);
foreach ($dom->getElementsByTagName('textarea') as $tag) {
    var_dump($tag->nodeValue);
}

Using a regex

If you are absolutely sure that the format of the markup will be consistent, a regex might work, too. To fix your regex, remove the extra spaces from the pattern:

preg_match("/[>](.*?)[<]/", $html, $matches);
var_dump($matches[1]);

Output:

string(33) "web scripting language of choice."

Demo

OTHER TIPS

Use strip_tags instead.

var_dump(strip_tags('<textarea width="500" >web scripting language of choice.</textarea>'));

This will do it:

<?
$string = '<textarea width="500" >web scripting language of choice.</textarea>';

$match = preg_replace('%<textarea width="500" >(.*?)</textarea>%i', '$1', $string );

echo $match;
//web scripting language of choice.
?>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top