如何获得字符串的一部分?(How to get part of a string?)

我有一个字符串:

$s = '<div data-x="0" data-y="0">Some text asdasdas</div> <div data-x="0" data-y="-1000">Some text...</div> <div data-x="0" data-y="-2000">Some text foobar</div> <div data-x="500" data-y="-3000">Some t..</div>';

字符串可以不同。 分区可以包括课程等。

如何从每个Div的data-xdata-y获得价值? 喜欢:

$x1 = 0; $y1 = 0; $x2 = 0; $y2 = -1000; $x3 = 0; $y3 = -2000; $x4 = 500; $y4 = -4000;

我想将字符串分解为数组,但后来我不知道该怎么做..

I have a string:

$s = '<div data-x="0" data-y="0">Some text asdasdas</div> <div data-x="0" data-y="-1000">Some text...</div> <div data-x="0" data-y="-2000">Some text foobar</div> <div data-x="500" data-y="-3000">Some t..</div>';

The string can be different. Divs can include classes and etc.

How to get value from data-x and data-y of each Div? Like:

$x1 = 0; $y1 = 0; $x2 = 0; $y2 = -1000; $x3 = 0; $y3 = -2000; $x4 = 500; $y4 = -4000;

I would like to explode the string into an array, but then I don't know what to do ..

最满意答案

为此使用适当的HTML解析器:

$dom = new DOMDocument; $dom->loadHTML( $s ); $xpath = new DOMXPath( $dom ); // xpath query: find all div nodes having a data-x and data-y attribute $nodes = $xpath->query( '//div[@data-x and @data-y]' ); $result = array(); foreach( $nodes as $node ) { $result[] = array( 'x' => (int) $node->getAttribute( 'data-x' ), 'y' => (int) $node->getAttribute( 'data-y' ), ); }

如果您真的想要$x<n>和$y<n>样式变量(其中<n>表示数字)作为结果(而不是结果数组),那么您可以用以下内容替换foreach循环:

for( $n = 1; $n <= $nodes->length; $n++ ) { ${'x' . $n} = (int) $node->getAttribute( 'data-x' ); ${'y' . $n} = (int) $node->getAttribute( 'data-y' ); }

我仍然建议将它们收集在阵列中; 这使得之后更容易处理。

Use a proper HTML parser for this:

$dom = new DOMDocument; $dom->loadHTML( $s ); $xpath = new DOMXPath( $dom ); // xpath query: find all div nodes having a data-x and data-y attribute $nodes = $xpath->query( '//div[@data-x and @data-y]' ); $result = array(); foreach( $nodes as $node ) { $result[] = array( 'x' => (int) $node->getAttribute( 'data-x' ), 'y' => (int) $node->getAttribute( 'data-y' ), ); }

If you truly want $x<n> and $y<n> style variables (where <n> represents a number) as a result (in stead of an array of results), then you could replace the foreach loop with something like:

for( $n = 1; $n <= $nodes->length; $n++ ) { ${'x' . $n} = (int) $node->getAttribute( 'data-x' ); ${'y' . $n} = (int) $node->getAttribute( 'data-y' ); }

I would still recommend collecting them in an array in stead though; makes for much easier processing afterwards.

如何获得字符串的一部分?(How to get part of a string?)

我有一个字符串:

$s = '<div data-x="0" data-y="0">Some text asdasdas</div> <div data-x="0" data-y="-1000">Some text...</div> <div data-x="0" data-y="-2000">Some text foobar</div> <div data-x="500" data-y="-3000">Some t..</div>';

字符串可以不同。 分区可以包括课程等。

如何从每个Div的data-xdata-y获得价值? 喜欢:

$x1 = 0; $y1 = 0; $x2 = 0; $y2 = -1000; $x3 = 0; $y3 = -2000; $x4 = 500; $y4 = -4000;

我想将字符串分解为数组,但后来我不知道该怎么做..

I have a string:

$s = '<div data-x="0" data-y="0">Some text asdasdas</div> <div data-x="0" data-y="-1000">Some text...</div> <div data-x="0" data-y="-2000">Some text foobar</div> <div data-x="500" data-y="-3000">Some t..</div>';

The string can be different. Divs can include classes and etc.

How to get value from data-x and data-y of each Div? Like:

$x1 = 0; $y1 = 0; $x2 = 0; $y2 = -1000; $x3 = 0; $y3 = -2000; $x4 = 500; $y4 = -4000;

I would like to explode the string into an array, but then I don't know what to do ..

最满意答案

为此使用适当的HTML解析器:

$dom = new DOMDocument; $dom->loadHTML( $s ); $xpath = new DOMXPath( $dom ); // xpath query: find all div nodes having a data-x and data-y attribute $nodes = $xpath->query( '//div[@data-x and @data-y]' ); $result = array(); foreach( $nodes as $node ) { $result[] = array( 'x' => (int) $node->getAttribute( 'data-x' ), 'y' => (int) $node->getAttribute( 'data-y' ), ); }

如果您真的想要$x<n>和$y<n>样式变量(其中<n>表示数字)作为结果(而不是结果数组),那么您可以用以下内容替换foreach循环:

for( $n = 1; $n <= $nodes->length; $n++ ) { ${'x' . $n} = (int) $node->getAttribute( 'data-x' ); ${'y' . $n} = (int) $node->getAttribute( 'data-y' ); }

我仍然建议将它们收集在阵列中; 这使得之后更容易处理。

Use a proper HTML parser for this:

$dom = new DOMDocument; $dom->loadHTML( $s ); $xpath = new DOMXPath( $dom ); // xpath query: find all div nodes having a data-x and data-y attribute $nodes = $xpath->query( '//div[@data-x and @data-y]' ); $result = array(); foreach( $nodes as $node ) { $result[] = array( 'x' => (int) $node->getAttribute( 'data-x' ), 'y' => (int) $node->getAttribute( 'data-y' ), ); }

If you truly want $x<n> and $y<n> style variables (where <n> represents a number) as a result (in stead of an array of results), then you could replace the foreach loop with something like:

for( $n = 1; $n <= $nodes->length; $n++ ) { ${'x' . $n} = (int) $node->getAttribute( 'data-x' ); ${'y' . $n} = (int) $node->getAttribute( 'data-y' ); }

I would still recommend collecting them in an array in stead though; makes for much easier processing afterwards.