简体   繁体   English

在PHP中的字符串中的textarea之间的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);

i want to return just "web scripting language of choice." 我只想返回“选择的Web脚本语言”。 form this string. 形成这个字符串。 please help me. 请帮我。 to arrive this PHP 到达这个PHP

Using a DOM parser 使用DOM解析器

HTML is not a regular language and cannot be correctly parsed using a regular expression. HTML不是常规语言,因此无法使用正则表达式正确解析。 Use a DOM parser instead. 请改用DOM分析器。 Here's how it can be done using PHP's DOMDocument class: 使用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);
}

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 演示

请改用strip_tags

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM