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" ?

Recommended Answers

All 5 Replies

Member Avatar for diafol

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);
}

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
Member Avatar for diafol

Doh!

$rawpieces = split('coords="',$file);
$check = array_shift($rawpieces);
foreach($rawpieces as $piece){
  $endcoord = strpos('"',$piece)
  $coords[] = substr($piece,0,$endcoord-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.

commented: ridiculously easy! +7

: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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.