English 中文(简体)
说明、定期表述中摘取变量?
原标题:Extracting variables from string, regular expression?

我的迷惑:作为新版的PHP,我试图利用定期表达方式从扼杀中提取一些数据,但我找不到正确的辛子。

插图内容被作为网站若干图像的html删除,我希望最终产出为3个精细变量:“$Number1”、“$Number2”和“$Status”。

说明投入内容的一个例子是:

<div id="system">         
<img alt="2" height="35" src="/images/numbers/2.jpg" width="18" /><img alt="2" height="35" src="/images/numbers/2.jpg" width="18" /><img alt=".5" height="35" src="/images/numbers/point5.jpg" style="margin-left: -4px" width="26" /><img alt="system statusA" height="35" src="/images/numbers/statusA.jpg" width="37" /><img alt="2" height="35" src="/images/numbers/2.jpg" width="18" /><img alt="1" height="35" src="/images/numbers/1.jpg" width="18" /><img alt=".0" height="35" src="/images/numbers/point0.jpg" style="margin-left: -4px" width="26" />
</div>

由此可见的可能价值观是:

  • 0.jpg
  • 1.jpg
  • 2.jpg
  • 3.jpg
  • 4.jpg
  • 5.jpg
  • 6.jpg
  • 7.jpg
  • 8.jpg
  • 9.jpg
  • point0.jpg
  • point5.jpg
  • statusA.jpg
  • statusB.jpg
  • statusC.jpg
  • statusD.jpg
  • statusE.jpg
  • statusF.jpg

结果是:

  • "Number1" (XX.X) based upon the first two numbers (0-9) and .0 or .5
  • "Status" (statusX) based upon the status
  • "Number2" (XX.X) based upon the last two numbers (0-9) and .0 or .5

至今为止的法典:

$regex =  alt= (.*?) ;
preg_match($regex,$html,$match);
var_dump($match);
echo $match[0];

或许,我不得不采取多种步骤或利用另一个职能,谁能帮助我?

问题回答

你们首先要问的是:“以什么形式是我的投入数据”。 既然在这种情况下,显然是一种超文本的幻灯,那么你就应当把这种ni子注入一个超文本的帽子,而不是固定的表达引擎。

我不知道确切的职能名称,但你的法典应当这样考虑:

$htmltext =  <div id="system">[...]</div> ;
$htmltree = htmlparser_parse($htmltext);
$images = $htmltree->find_all( img );
foreach ($images as $image) {
  echo $image->src;
}

因此,你需要找到一个超文本帽子,把一个str子树起来。 这些节点应该有办法,根据特别安全局的班级、元件名称或代号,在课堂内找到节点。 这座图书馆的名称是BeautifulSoup,对 Java来说,它是JSoup,我确信PHP也有类似之处。

实例见simplehtmldom。 前景看好。

You want just the alt s? Try this xpath example:

$doc = new DOMDocument();
$doc->loadHTML($html);
$xpath = new DomXpath($doc);

foreach($xpath->query( //img/@alt ) as $node){
    echo $node->nodeValue."
";
}




相关问题
Simple JAVA: Password Verifier problem

I have a simple problem that says: A password for xyz corporation is supposed to be 6 characters long and made up of a combination of letters and digits. Write a program fragment to read in a string ...

Case insensitive comparison of strings in shell script

The == operator is used to compare two strings in shell script. However, I want to compare two strings ignoring case, how can it be done? Is there any standard command for this?

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

String initialization with pair of iterators

I m trying to initialize string with iterators and something like this works: ifstream fin("tmp.txt"); istream_iterator<char> in_i(fin), eos; //here eos is 1 over the end string s(in_i, ...

break a string in parts

I have a string "pc1|pc2|pc3|" I want to get each word on different line like: pc1 pc2 pc3 I need to do this in C#... any suggestions??

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签