如何从html和javascript标签和函数中获取文本


How to get text from html and javascript tags and functions

大家好,我想从这个代码中获得文本

$content = '<span class="version_host">
   <script type="text/javascript">
      document.writeln('streamin.to');
   </script>
</span>';

我想在('streamin.to')流到之间获取文本

我正在使用php 的strip_tags()函数

$test = strip_tags($content);
echo $test;

输出:

document.writeln('streamin.to');

请帮帮我,我只想把文本流输入到。

一种方法是使用str_replace

<?php
$content = "<span class='version_host'>
   <script type='text/javascript'>
      document.writeln('streamin.to');
   </script></span>";
$test = strip_tags($content);
$test1 = str_replace("');","",str_replace("document.writeln('","",$test));
echo $test1;
?>

或使用预匹配

<?php
  $content = "<span class='version_host'>
       <script type='text/javascript'>
          document.writeln('streamin.to');
       </script></span>";
    $test = strip_tags($content);
$data = preg_match("/''([^'']*?)''/", $test, $matches);
echo $matches[1];

或使用@Rene Pot 所述的爆炸

<?php
 $content = "<span class='version_host'>
           <script type='text/javascript'>
              document.writeln('streamin.to');
           </script></span>";
$test = strip_tags($content);
$array = explode("'",$test);
$string = $array[1];
echo $string;
?>

希望这能帮助您

以下内容应能满足您的需求。

$content = "<span class='version_host'>
   <script type='text/javascript'>
      document.writeln('streamin.to');
   </script>
</span>";
$test = strip_tags($content);
$array = explode("'", $test);
echo $array[1];

使用explode,您可以用'分隔符分割字符串"document.writeln('streamin.to')",得到一个由3个元素组成的数组。

strip_tags()所做的是去掉String中的任何<>标记,因此document.writeln('streamin.to');仍然存在。

现在,您只想得到单引号之间的部分,所以最好使用正则表达式(在regExOne.com上了解更多信息)

output = "document.writeln('streamin.to');".match(/'([^']+)'/);

然后output[1]将包含您要查找的内容。

编辑:如果您想使用php实现同样的效果,请尝试

preg_match("/'([^']+)'/", "document.writeln('streamin.to');", $output);

在这种情况下,$output[1]将包含您要查找的