Jump to content

Shortening this code


-Karl-

Recommended Posts

Okay, so I have this code:

// Attack
$startatt = strpos($content,'alt="Attack"/>')+14;
$endatt = strpos($content,'<a id="skill2"',$startatt);
$attack = substr($content,$startatt,$endatt-$startatt);

// Defence
$startdef = strpos($content,'alt="Defence"/>')+15;
$enddef = strpos($content,'<a id="skill3"',$startdef);
$defence = substr($content,$startdef,$enddef-$startdef);

 

There's around 23 more of them, is there any way I could create a loop to do this, instead of writing them one by one?

 

Something similar to this:

string[] skills = 	{Attack,Defence,Strength,Ranged,Prayer,Magic,Runecrafting,Construction,Constitution,
		Agility,Herblore,Thieving,Crafting,Fletching,Slayer,Hunter,Mining, Smithing,Fishing,
		Cooking,Firemaking,Woodcutting,Farming,Summoning,Dungeoneering}

int[] StrPos = {14,15,16,20,14,14,13,15,19,17,15,18,16,16,14,16,14,15,20,14,20,17,21}

int[] level = {2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24}

string[] abbre = {att,def,str,ran,pra,mag,run,cons,con,agi,her,thi,cra,fle,sla,hun,smi,fis,coo,fir,woo,far,sum,dun}

for(int i = 0;i skills.length;i++) {
$start+abbre[i] = strpos($content,'alt="+skills[i]"/>')+StrPos[i];
$end+abbre[i] = strpos($content,'<a id="+level[i]"',$start);
$+skills = substr($content,$start+abbre[i],$end+abbre[i]-$start+abbre[i]);
}

 

If so, how?

Link to comment
https://forums.phpfreaks.com/topic/201454-shortening-this-code/
Share on other sites

Basically, I'm using curl to grab data from an external source, to which I have consent to use. The only method I've found to work is the strpos function.

 

Is there another method which would be more effective?

 

Such as utilizing substr?

 

$testatt = substr($content,'<img src="http://www.runescape.com/img/hiscores/skill_icon_Attack1.gif" alt="Attack"/>', -2);

 

Obviously that won't work as I'm not sure how to define a starting position using substr.

 

 

Anyway, once the data has been grabbed, it's then stored in a database.

	$ch = curl_init() or die(curl_error());

curl_setopt($ch, CURLOPT_URL,'http://services.runescape.com/m=adventurers-log/display_player_profile.ws?searchName=lnsult');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$raw = curl_exec($ch) or die(curl_error());

curl_close($ch); 

$content = str_replace($newlines, "", html_entity_decode($raw));

		// Attack 
			$startatt = strpos($content,'alt="Attack"/>')+14;
			$endatt = strpos($content,'<a id="skill2"',$startatt);
			$attack = substr($content,$startatt,$endatt-$startatt);

 

$attack would output the players level. It all works fine, but I'm just looking for a more effective way to do it.

 

 

Is there a right(string,2) such as in mSL, the substr looks similar, but I'm unsure if it will produce the same result.

 

If I am understanding your question correctly and looking at the source returned by the curl function, you're trying to parse the following lines:

<img src="http://www.runescape.com/img/hiscores/skill_icon_Attack1.gif" alt="Attack"/>99
<img src="http://www.runescape.com/img/hiscores/skill_icon_Defence1.gif" alt="Defence"/>99
<img src="http://www.runescape.com/img/hiscores/skill_icon_Strength1.gif" alt="Strength"/>99
<img src="http://www.runescape.com/img/hiscores/skill_icon_Constitution1.gif" alt="Constitution"/>99
<img src="http://www.runescape.com/img/hiscores/skill_icon_Ranged1.gif" alt="Ranged"/>92
<img src="http://www.runescape.com/img/hiscores/skill_icon_Prayer1.gif" alt="Prayer"/>80
<img src="http://www.runescape.com/img/hiscores/skill_icon_Magic1.gif" alt="Magic"/>99
<img src="http://www.runescape.com/img/hiscores/skill_icon_Cooking1.gif" alt="Cooking"/>99
<img src="http://www.runescape.com/img/hiscores/skill_icon_Woodcutting1.gif" alt="Woodcutting"/>99
<img src="http://www.runescape.com/img/hiscores/skill_icon_Fletching1.gif" alt="Fletching"/>99
<img src="http://www.runescape.com/img/hiscores/skill_icon_Fishing1.gif" alt="Fishing"/>99
<img src="http://www.runescape.com/img/hiscores/skill_icon_Firemaking1.gif" alt="Firemaking"/>86
<img src="http://www.runescape.com/img/hiscores/skill_icon_Crafting1.gif" alt="Crafting"/>87
<img src="http://www.runescape.com/img/hiscores/skill_icon_Smithing1.gif" alt="Smithing"/>83
<img src="http://www.runescape.com/img/hiscores/skill_icon_Mining1.gif" alt="Mining"/>85
<img src="http://www.runescape.com/img/hiscores/skill_icon_Herblore1.gif" alt="Herblore"/>81
<img src="http://www.runescape.com/img/hiscores/skill_icon_Agility1.gif" alt="Agility"/>75
<img src="http://www.runescape.com/img/hiscores/skill_icon_Thieving1.gif" alt="Thieving"/>76
<img src="http://www.runescape.com/img/hiscores/skill_icon_Slayer1.gif" alt="Slayer"/>81
<img src="http://www.runescape.com/img/hiscores/skill_icon_Farming1.gif" alt="Farming"/>83
<img src="http://www.runescape.com/img/hiscores/skill_icon_Runecrafting1.gif" alt="Runecrafting"/>72
<img src="http://www.runescape.com/img/hiscores/skill_icon_Hunter1.gif" alt="Hunter"/>70
<img src="http://www.runescape.com/img/hiscores/skill_icon_Construction1.gif" alt="Construction"/>70
<img src="http://www.runescape.com/img/hiscores/skill_icon_Summoning1.gif" alt="Summoning"/>1
<img src="http://www.runescape.com/img/hiscores/skill_icon_Dungeoneering1.gif" alt="Dungeoneering"/>9

to get the string in the "alt" attribute and the number following the tag. All those lines are similar enough that a regular expression should be able to work. I suggest you post a question in the regular expression subsection and ask how to do it.

 

Ken

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.