0

i want to find all occurrence of a string: "coords"" within another, but i only can found the first and the last
( stripos() and strripos() )

<body>  
<img src="page.jpg" border="0" usemap="#fghf"  />
<map name="fghf" id="fghf">
<area shape="rect" coords="10,71,258,236" href="#" alt="some_alt" title="some_title" />
<area shape="rect" coords="258,72,506,237" href="#" alt="some_alt" title="some_title" />
<area shape="rect" coords="155,79,150,200" href="#" alt="some_alt" title="some_title" />
<area shape="rect" coords="88,22,400,217" href="#" alt="some_alt" title="some_title" />
</map> 
</body>

i have manage to extract one of them:

//get coords
	$pos = strpos($file,"coords=\"");
	$tmpfile = substr($file,$pos+8);
	list($tmpfile) = split(" ",$tmpfile);
	$tmpfile_len = strlen($tmpfile);
	$coords = substr($tmpfile,0,$tmpfile_len-1);

but, how can i loop on all the strings:"coords" ?

Edited by ofir0803: n/a

3
Contributors
5
Replies
8
Views
6 Years
Discussion Span
Last Post by ofir0803
0

Although I don't understand it myself, it sounds like a job for regex.

Otherwise, something like this:

$rawpieces = split('coords="',$file);
$pieces = array_shift($rawpieces);
foreach($pieces as $piece){
  $endcoord = strpos('"',$piece)
  $coords[] = substr($piece,0,$endcoord-1);
}
0

Although I don't understand it myself, it sounds like a job for regex.

Otherwise, something like this:

$rawpieces = split('coords="',$file);
$pieces = array_shift($rawpieces);
foreach($pieces as $piece){
  $endcoord = strpos('"',$piece)
  $coords[] = substr($piece,0,$endcoord-1);
}
Warning: Invalid argument supplied for foreach() in C:\wamp\www\test\convert.php on line 58
0

Doh!

$rawpieces = split('coords="',$file);
$check = array_shift($rawpieces);
foreach($rawpieces as $piece){
  $endcoord = strpos('"',$piece)
  $coords[] = substr($piece,0,$endcoord-1);
}
1

If your input is always going to be HTML/XML this would be ridiculously easy using SimpleXML and an xpath query:

<?php

$string = <<<XML
<body>  
<img src="page.jpg" border="0" usemap="#fghf"  />
<map name="fghf" id="fghf">
<area shape="rect" coords="10,71,258,236" href="#" alt="some_alt" title="some_title" />
<area shape="rect" coords="258,72,506,237" href="#" alt="some_alt" title="some_title" />
<area shape="rect" coords="155,79,150,200" href="#" alt="some_alt" title="some_title" />
<area shape="rect" coords="88,22,400,217" href="#" alt="some_alt" title="some_title" />
</map> 
</body>
XML;

$xml = new SimpleXMLElement( $string );
$results = $xml->xpath('//area/@coords');

foreach($results as $coords){
	echo $coords.PHP_EOL;
}

You can supply the html/xml as a string any way you wish. Then using an xpath query, we find the coords attribute (@coords) of all area elements in the document.

Votes + Comments
ridiculously easy!
0

:icon_smile:

If your input is always going to be HTML/XML this would be ridiculously easy using SimpleXML and an xpath query:

<?php

$string = <<<XML
<body>  
<img src="page.jpg" border="0" usemap="#fghf"  />
<map name="fghf" id="fghf">
<area shape="rect" coords="10,71,258,236" href="#" alt="some_alt" title="some_title" />
<area shape="rect" coords="258,72,506,237" href="#" alt="some_alt" title="some_title" />
<area shape="rect" coords="155,79,150,200" href="#" alt="some_alt" title="some_title" />
<area shape="rect" coords="88,22,400,217" href="#" alt="some_alt" title="some_title" />
</map> 
</body>
XML;

$xml = new SimpleXMLElement( $string );
$results = $xml->xpath('//area/@coords');

foreach($results as $coords){
	echo $coords.PHP_EOL;
}

You can supply the html/xml as a string any way you wish. Then using an xpath query, we find the coords attribute (@coords) of all area elements in the document.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.