php中字符串中文本区域之间的preg_match


preg_match between textarea in string in php

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

我只想从这个字符串中返回"选择的web脚本语言"。请帮帮我。到达这个PHP

使用DOM解析器

HTML不是一种正则语言,无法使用正则表达式正确解析。请改用DOM解析器。以下是如何使用PHP的DOMDocument类:

$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);
}

使用正则表达式

如果您绝对确信标记的格式是一致的,那么regex也可能起作用。要修复正则表达式,请从模式中删除多余的空格:

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

输出:

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

演示

请改用strip_tags

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

这样做:

<?
$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.
?>