简体   繁体   English

preg_match获取文本

[英]preg_match get text

I have test.php and on test1.php i have this php code running 我有test.phptest1.php我运行这个PHP代码

<?php 
$Text=file_get_contents("http://inviatapenet.gethost.ro/sop/test.php");
 preg_match_all('~fid="(.*?)"~si',$Text,$Match);
 $fid=$Match[1][1];
 echo $fid;
?>

what i want to do is to get the text from test.php 我想要做的是从test.php获取文本

from this fid='gty5etrf' JavaScript an i need just the content of fid 从这个fid ='gty5etrf'JavaScript我只需要fid的内容

<script type='text/javascript'>fid='gty5etrf'; v_width=620; v_height=490;</script><script type='text/javascript' src='http://www.reyhq.com/player.js'></script>

in the test1.php i need to show only the content 在test1.php中,我只需要显示内容

gty5etrf

what i have to do? 我该怎么办?

您可以尝试表达式fid\\=\\'([^\\']+)\\'因为[^\\']+使表达式以非正确的方式非贪婪,同样,表达式错误,因为它正在寻找双引号而不是单引号。

 preg_match_all('/fid=\'([^\']+)\'/',$Text,$Match);

Your regex was wrong. 你的正则表达式错了。 First, you were looking for fid="..." instead of fid='...' . 首先,你在寻找fid="..."而不是fid='...' Second, with .* , the regex would match any character further than the end of the fid attribute. 其次,使用.* ,正则表达式将匹配除fid属性末尾之外的任何字符。

Here is the full code : 这是完整的代码:

preg_match_all('/fid=\'([^\']+)\'/',$Text,$Match);
$fid=$Match[1][0];
echo $fid;

And this should be 这应该是

$fid=$Match[1][0];

instead of : 代替 :

$fid=$Match[1][1];

Matching string inside '' : '(?:[^\\\\']*|\\\\.)*' '''(?:[^\\\\']*|\\\\.)*'匹配字符串'(?:[^\\\\']*|\\\\.)*'

Matching string inside "" : "(?:[^\\\\"]*|\\\\.)*" ""匹配字符串: "(?:[^\\\\"]*|\\\\.)*"

Both of them (ignoring spaces): fid\\s*=\\s*('(?:[^\\\\']*|\\\\.)*'|"(?:[^\\\\"]*|\\\\.)*") 两者(忽略空格): fid\\s*=\\s*('(?:[^\\\\']*|\\\\.)*'|"(?:[^\\\\"]*|\\\\.)*")

And escaped for php: 并为php转义:

$regexp = '~fid\\s*=\\s*(\'(?:[^\\\\\']*|\\\\.)*\'|"(?:[^\\\\"]*|\\\\.)*")~';

This will handle correctly even this: 这将正确处理,即使这样:

fid  = 'foo\'s bar';

a short pattern: 一个简短的模式:

$pattern = '~\bfid\s*=\s*["\']\K\w+~';

or a long pattern: 或长模式:

$pattern = '~<script[^>]*>(?:[^f<]+|\Bf+|f(?!id\b)|<+(?!/script>))*+\bfid\s*=\s*(["\'])\K[^"\']+(?=\1)~';

the result with 结果

preg_match($pattern, $Text, $match);
$fid = $match[0];

The short pattern finds sequences like: 短模式找到如下序列:

fid='somechars
fid  = "somecchars

The long pattern does the same but also checks you are between script tags. 长模式执行相同但也检查脚本标记之间。


Using XPath: 使用XPath:

$html = <<<'EOD'
<script type='text/javascript'>fid='gty5etrf'; v_width=620; v_height=490;</script><script type='text/javascript' src='http://www.reyhq.com/player.js'></script>
EOD;

$dom = new DOMDocument;
libxml_use_internal_errors(true);
$dom->loadHTML($html);
$xp = new DOMXPath($dom);
$query = <<<'EOD'
    substring-before(
        substring-after(
            //script[contains(., "fid='")],
            "fid='"
        ),
        "'"
    )
EOD;

echo $xp->evaluate($query);

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

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